1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need IP blocker Script

Discussion in 'PHP' started by doubler, Jul 22, 2007.

  1. #1
    I need a script from which i could block IPs manually. so anybody who could help me for this?
     
    doubler, Jul 22, 2007 IP
  2. mgware

    mgware Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Attached.
    In the file ips.txt you have to put the banned ips.
    If you want, I can make a system to administrate banned IPs via web, and encrypt the file ips.txt.
     

    Attached Files:

    mgware, Jul 22, 2007 IP
  3. ds316

    ds316 Peon

    Messages:
    154
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A slight extension of mgware's script, this allows wildcard banning with * characters (eg 63.62.32.* bans 63.62.32.0-63.62.32.255), and can be included at the top of your PHP page like (provided you saved the below code as ipcheck.php obviously):

    include 'ipcheck.php';

    <?
    if (file_exists("ips.txt"))
    {
    $ipsBanned = file_get_contents("ips.txt");
    $ipsBanned = str_replace(array("\r\n","\n",".","*","()?"), array(")?(",")?(","\\.","[\\d\\.]*",""),"^(".$ipsBanned.")?$");
    $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
    if (preg_match($ipsBanned,$ip)){
    die("Banned IP.");
    }
    ?>
    PHP:
     
    ds316, Jul 22, 2007 IP
  4. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Only a TIP: When you detect a banned IP, you should not return a 404 HTTP error, nor even a "Banned IP" message. That way you're telling the banned user that you catch them, and then he/she can access to your site using a proxy. Is better if you display a generic page with no useful info.
     
    ajsa52, Jul 22, 2007 IP
  5. doubler

    doubler Active Member

    Messages:
    952
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Thank you very much for your kind co-operation. can you setting for this IP >>> 210.2.154.240
     
    doubler, Jul 22, 2007 IP
  6. medusa

    medusa Peon

    Messages:
    349
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Instaed of having the IP lists in a txt file, I am storing them in a database table as a serialized array. This is how it looks like:
    s:8:"ip_block";a:2:{i:0;s:9:"127.0.0.2";i:1;s:12:"129.23.23.45";}
    PHP:
    Now, I call the database on top of the file and then add:
    $ipsBanned     = $CFG['ip_block'];
    PHP:
    How do I proceed from here? I want to use ds316's code above. It works perfectly if from a text file, I am just stuck as I want it with a database...

    This part needs some modification:
    $ipsBanned = str_replace(array("\r\n","\n",".","*","()?"), array(")?(",")?(","\\.","[\\d\\.]*",""),"^(".$ipsBanned.")?$");
    $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
    if (preg_match($ipsBanned,$ip)){
    die("Banned IP.");
    }
    PHP:
    I appreciate any clues.
     
    medusa, Jul 28, 2007 IP
  7. ds316

    ds316 Peon

    Messages:
    154
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    After
    $ipsBanned     = $CFG['ip_block'];
    PHP:
    add and replace
    $ipsBanned = unserialize($ipsBanned);
    $ipsBanned = implode(")?(",$ipsBanned);
    $ipsBanned = str_replace(array(".","*","()?"), array("\\.","[\\d\\.]*",""),"^(".$ipsBanned.")?$");
    $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
    
    if (preg_match($ipsBanned,$ip)){
      die("Banned IP.");
    }
    PHP:
    Assuming you haven't unserialized the data beforehand.
     
    ds316, Jul 31, 2007 IP
  8. medusa

    medusa Peon

    Messages:
    349
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you ds316!

    The above code was not really perfect, but I contacted ds316 by PM and it was fixed to my satisfaction.

    He was quick and neat. Thank you once again.

    medusa
     
    medusa, Aug 1, 2007 IP
  9. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #9
    use $_SERVER['REMOTE_ADDR'] instead. And just check like this:

    if($ip == $array_from_db){
    display_generic_page();
    }
     
    Tarkan, Aug 1, 2007 IP