Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   Banned IP Redirecting[PHP] and [mySQL] (http://www.go4expert.com/forums/banned-ip-redirectingphp-mysql-t28906/)

The Alchemist 17Aug2012 16:46

Banned IP Redirecting[PHP] and [mySQL]
 
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 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
 
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 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 :
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 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
  
exit;
 }
   echo  
'<html><title>Welcome></title><body><h1>Actual  contents of page</h1></body></html>';
 
?>

Th
ats it...

Please point out mistakes if you find some in my codes..
Please don't forget to give feedback!!:happy:

shabbir 17Aug2012 19:28

Re: Banned IP Redirecting[PHP] and [mySQL]
 
Instead of wasting your resource processing PHP for banned IP, it is better to deny them using .htaccess

The Alchemist 17Aug2012 23:24

Re: Banned IP Redirecting[PHP] and [mySQL]
 
Quote:

Originally Posted by shabbir (Post 97000)
Instead of wasting your resource processing PHP for banned IP, it is better to deny them using .htaccess

Well... I didnt know that... Im not a pro...


All times are GMT +5.5. The time now is 21:09.