Instructions For IP Blocking Script

You need PHP 5 installed onto your server for the following script to work. You can check to see if this was done by creating a file such as phpinfo.php with the following code contained within.

<? phpinfo(); ?>

This blocking script consists of 2 files and a piece of code to enter into each page you wish to use and a redirect page. Name the first file blocker.php and enter the following code into it.

<?
//begin config section
$banaddress = "banned.htm"; //this is your redirect page
$file = "blocklist.xml"; //this is your xml file with banned addresses
//end config section
$xmlstr=file_get_contents("$file");
$xml=new SimpleXMLElement($xmlstr);
$i=0;
$a=array();
$r=array();
foreach ($xml->id as $id) {
   $ip=$xml->id[$i]->ip;
   $ip2=$xml->id[$i]->ip2;
   if ($ip2!=""){
      array_push($r, "$ip#$ip2");
   }
   else{
      array_push($a, "$ip");
   }
   $i++;
}
$n=count($r);
$i=0;
while($i<$n){
   $preip12 = explode("#", $r["$i"]);
   $ip1 = $preip12[0];
   $ip2 = $preip12[1];
   $preadd1 = explode(".", $ip1);
   $first1 = $preadd1[0];
   $second1 = $preadd1[1];
   $third1 = $preadd1[2];
   $fourth1 = $preadd1[3];
   $preadd2 = explode(".", $ip2);
   $first2 = $preadd2[0];
   $second2 = $preadd2[1];
   $third2 = $preadd2[2];
   $fourth2 = $preadd2[3];
   $firste = $first2-$first1;
   $seconde = $second2-$second1;
   $thirde = $third2-$third1;
   $fourthe = $fourth2-$fourth1;
   $firstf = $first1;
   $secondf = $second1;
   $thirdf = $third1;
   $fourthf = $fourth1;
   $i1 = 0;
   $i2 = 0;
   $i3 = 0;
   $i4 = 0;
   while($firste>=$i1){
      $firstf = $i1+$first1;
      $rangerip = $firstf.".".$secondf.".".$thirdf.".".$fourthf;
      array_push($a, "$rangerip");
      $i2 = 0;
      $i1++;
      while($seconde>=$i2){
         $secondf = $i2+$second1;
         $rangerip = $firstf.".".$secondf.".".$thirdf.".".$fourthf;
         array_push($a, "$rangerip");
         $i3 = 0;
         $i2++;
         while($thirde>=$i3){
            $thirdf = $i3+$third1;
            $rangerip = $firstf.".".$secondf.".".$thirdf.".".$fourthf;
            array_push($a, "$rangerip");
            $i4 = 0;
            $i3++;
            while($fourthe>=$i4){
               $fourthf = $i4+$fourth1;
               $rangerip = $rangerip = $firstf.".".$secondf.".".$thirdf.".".$fourthf;
               array_push($a, "$rangerip");
               $i4++;
            }
         }
      }
   }
   $i++;
}
$visitorip=$_SERVER['REMOTE_ADDR'];
$n=count($a);
$i=0;
while($i<$n){
   if($a["$i"] == $visitorip){
      echo "<script type=\"text/javascript\">top.window.location.href=\"$banaddress\";</script>";
      exit;
   }
   $i++;
}
?>

As you can see there is a configurable section to change the location of your list of banned IP's and to the redirect page. It is not nessasary to change these paramaters unless you wish to do so.

Next create a file named blocklist.xml and enter the following code into it.

<?xml version="1.0" encoding="iso-8859-1"?>
<blocklist>
   <id>
      <ip>68.125.109.223</ip>
   </id>
   <id>
      <ip>68.125.109.223</ip>
      <ip2>68.125.110.255</ip2>
   </id>
</blocklist>

To add other banned addresses simply add <id><ip>XXX.XXX.XXX.XXX</ip></id> inbetween the <blocklist> and </blocklist> tags, where XXX.XXX.XXX.XXX is the IP address of the individual you would like to ban. If you would like to ban a range add the <ip2>XXX.XXX.XXX.XXX</ip2> tag after the inital <ip>XXX.XXX.XXX.XXX</ip> tag.

Add this code to every page you would like the blocker to work on.

<iframe src="blocker.php" width="0" height="0" frameborder="0" scrolling="no"></iframe>

Finally create a page named banned.htm and set it up so the user see's a message of some sort.