08-16-2012, 11:59 AM
BANNED IP REDIRECTING CODE
Hello Hack Community...
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.
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 Code:
<?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 counter
if($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
}
else //Otherwise, show the original contents of the page
{
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 Code:
<?php
//Code made by The Alchemist
//Welcome the prestigious banned ips with this page
echo '<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 :
Quote: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 Code:
<?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
}
else //Show actual contents if the user is not banned
{
echo '<html><title>Welcome></title><body><h1>Actual contents of page</h1></body></html>';
}
?>
Thats it...
Enjoy..
[b]Please don't forget to give feedback!!:cool:[/b]