Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question display info depending on IP

    Hi, I know next to nothing about PHP. One simple snippet I need.
    In plain english something like this:

    if ip other than "12.34.56.*" or "23.45.*.*"
    then continue displaying page.
    if not
    then display "my text" and stop(or redirect)


    (i need this to block spammers destroying my wordpress blog (or leaving me with hundreds of comments to moderate every day). there are only a few of them, so this seems like the best solution. before you suggest a different approach, please give pointers on the above)

    Thanks a million
    I know that I know nothing

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:

    $ip 
    $_SERVER['REMOTE_ADDR'];
    if (
    $ip == "12.34.56.*" || "23.45.*.*")
    {
    // your page here for these address's
    }
    {
    else
    // normal page here

    Last edited by rafiki; 11-11-2006 at 08:15 AM.

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ofcourse it has to be this (seems like a small typo with the { of the else function
    PHP Code:
    $ip $_SERVER['REMOTE_ADDR'];
    if (
    $ip == "12.34.56.*" || "23.45.*.*")
    {
    // your page here for these address's
    }
    else{
    // normal page here


  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The if(...) statement provided in the above code does not work for two reasons -

    Firstly, you cannot OR || values together and then test them against a value.

    Secondly, you cannot directly use a simple == test and get a wild card character * match to function.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes ofcourse, missed those errors
    This compares correctly, except for the wildcard
    PHP Code:
    $ip $_SERVER['REMOTE_ADDR']; 
    if (
    $ip == "12.34.56.*" || $ip=="23.45.*.*"

    // your page here for these address's 

    else{ 
    // normal page here 


  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The following tested code will work for the IP address ranges you listed -
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR']; 
    if(
    preg_match("#^(12\.34\.56|23\.45)\.#"$ip)) 
    {
    // found a match
    echo "Bad IP"// in the real code, don't echo anything, see the note following the code.
    exit();
    }
    // did not find a match
    echo "Continue the remainder of your normal code here";
    ?>
    Note: In real life, you probably don't want to echo anything for a banned IP address. The person abusing your web site will simply go through a different IP address. If you simply do an exit(), they might think the site is broken and stop abusing it.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    maybe the code below helps you, but note that i'm a noob myself so it could be totally wrong as well!
    As i don't want to claim code to be mine if it aint : i found (the biggest part of) the code at http://www.thescripts.com/forum/thread2146.html

    Now the code:
    PHP Code:
    <?
    function checkip($ip1){
     
    $ip2 '127.*.*.*';
     
    $numbers1 explode ('.'$ip1);
     
    $numbers2 explode ('.'$ip2);
     for (
    $i 0$i 4$i++) {
     if ((
    $numbers1[$i] == $numbers2[$i]) or
      (
    $numbers1[$i] == '*') or
      (
    $numbers1[$i] == '*')) {
      
    $match true;
     } else {
      
    $match false;
    break;
    }
    return 
    $match;
    }


    $ip $_SERVER['REMOTE_ADDR']; 
    function 
    checkip($ip);
     
    if (
    $match)  
    {  
    // Your page for these adresses here  
    }  
    else{  
    //normal page here
    }  
    ?>
    Note that i haven't tested it so it could have errors!
    Last edited by arne2; 11-11-2006 at 05:31 PM.

  • #8
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lol, didn't knew CFma was also typing a solution (which is better anyway i think).
    Therefore, just use his instead of mine !

    Note: In real life, you probably don't want to echo anything for a banned IP address. The person abusing your web site will simply go through a different IP address. If you simply do an exit(), they might think the site is broken and stop abusing it.
    => My response to that: you can make it even better ! What about putting some mysql_errors (like couldn't connect to the database) or something in the echo, they will then think your site is broken. Or put some other errors. Or put in 'offline' or 'under construction' or something else. That might work even better than a white screen! Or redirect them to another website, like www.anonexistingsite.com. Then it really looks offline (you can type a 'we have moved to .. ' message in the echo as well) As you can see, loads of ideas!

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The following array based code also works and allows you to easily modify the list of banned IP addresses -
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR']; 
    $banned = array();
    $banned[] = "12.34.56.";
    $banned[] = "23.45.";
    // add other IP's as needed

    $found FALSE;
    foreach(
    $banned as $value)
    {
        if(
    strpos($ip$value) === 0){
            
    // found a match starting in position 0
            
    $found TRUE;
            break;
        }
    }
    if(
    $found
    {
        
    // found a match
        
    echo "Bad IP";
        exit();
    }
    // did not find a match
    echo "Continue the remainder of your normal code here";
    ?>
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    New Coder
    Join Date
    Aug 2006
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all so very much!
    Great scripts and ideas!


    PS: everything works just the way I wanted
    Last edited by graficus; 11-12-2006 at 06:22 PM.
    I know that I know nothing


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •