PHPSEEKER.ORG

A free resort for PHP Developers

PHP ::: Block IP system (no need of database)

Math and logical operations....

PHP ::: Block IP system (no need of database)

Postby toshio » Tue Apr 20, 2010 2:57 pm

First, create a text file and name it list.txt

Add the following dummy IPs: 111.111.*, 222.222.222.222,333.*,444.444.444.*

Than, create a PHP file and add the following code:

Code: Select all
function checkSecurity ()
{
   // block ip file list (.inc)
   $list = 'list.txt'; // change this to your list patch
   $deny = array();
   
   // open and read file
   $fo = fopen($list, 'r');
   $str = fread($fo, filesize($list));
   fclose($fo);
      
   $str = str_replace(",",'_',$str);
   $ary = explode('_',$str);
   
   for($i=0; $i < count($ary); $i++)
   {
      $deny[] = $ary[$i];
   }
      
   foreach($deny as $ip) {
      if(eregi($ip,$_SERVER['REMOTE_ADDR'])) {
         header("Location: http://google.com?q=fuckyou"); // change this for whatever you want.
         exit();
      }
    }
}


usage:

DON'T FORGET TO INCLUDE THE FUNCTION FILE BEFORE CALLING THE FUNCTION!
Code: Select all
require_once("ip_block.php");
checkSecurity();


As you can see, the list accepts wild card as well to include a range of IPs.
If the IP is found, the function redirects the user to whatever you set on [ header() ].

Have fun!
PHPSEEKER.ORG Moderator
toshio
Site Admin
 
Posts: 34
Joined: Sat Dec 13, 2008 8:22 pm

Return to PHP Math

Who is online

Users browsing this forum: No registered users and 1 guest

cron