BANNED IP REDIRECTING CODE Hello everyone... The purpose of this code is to prevent users from visiting pages if their ip addresses are banned. Using this code, you can ban IP addresses from visiting your site. It can also be useful for preventing proxy users and VPN users from visiting your site(if you have a good list that contains IP addresses used by VPNs and proxy servers). I've got two codes, one in which the banned IP addresses are stored in a txt file named list.txt, and another code i which the banned IP addresses are stored in a database name mydb with a table named banned and column named ip. 1. Code in which the banned IP addresses are stored in list.txt file : PHP: <?php//Code made by The Alchemist$filename="list.txt";//All the banned IP addresses stored in this file$ip=$_SERVER['REMOTE_ADDR']; // ip address of the user$count=0;//setting a counterif($file_exists($filename)){ $file=fopen($filename,'r');//only reading contents of file while(!feof($file)) //checking contents of file till the end { $reading=fgets($file); //checking contents of file line by line if($ip==$reading) //if IP of user matches with any among the list { $count=1;//setting the counter if value matches break; } } fclose($file);}if($count!=0){ header('Location : forbanned.php'); //redirecting to the page for banned members exit;} echo '<html><title>Welcome></title><body><h1>Actual contents of page</h1></body></html>';?> Now, the page where the prestigious users with banned IP addresses are redirected(This is the forbanned.php thats redirected to in my codes) : PHP: <?php//Code made by The Alchemist//Welcome the prestigious banned ips with this pageecho '<html><title>For the banned</title><body><h1>You are banned from this site.</br>Get Lost!!</body></html>';?> Remember, the IP addresses should be stored in the list.txt file in this way : Code: 127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.4 2. Code in which the banned IP addresses are stored in a mySQL database : PHP: <?php //Code made by The Alchemist $ip=$_SERVER['REMOTE_ADDR'];//Finding IP address of user $hostname='localhost';//assuming host is localhost $user='admin';//assuming username to be admin $password='password';//assuming password to be password $dbname='mydb';//assuming database name to be mydb $con=mysql_connect($hostname, $user, $password) or DIE('Connection to host failed'); mysql_select_db($dbname,$con) or DIE('Database name is not available!'); $query="SELECT * from banned where ip='$ip'";//table name is banned and column name is ip $chk=mysql_query($query,$con); $res=mysql_num_rows($chk); if($res!=0) //If the user's ip is among the ones banned listed in the database { header('Location : forbanned.php');//redirect to the page for banned members exit; } echo '<html><title>Welcome></title><body><h1>Actual contents of page</h1></body></html>'; ?> Thats it... Please point out mistakes if you find some in my codes.. Please don't forget to give feedback!!:happy:
Instead of wasting your resource processing PHP for banned IP, it is better to deny them using .htaccess