Block IP with Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xsuck91
Forum Newbie
Posts: 2
Joined: Fri Oct 21, 2011 9:59 am

Block IP with Database

Post by xsuck91 »

how to block someone from visiting my page using ip adress ?
this is the case
if someone come to my site then we record the ip address and put it into database.
when the visitor come again with the same IP they cannot see my page (block them) and redirect it to somewhere page.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Block IP with Database

Post by Celauran »

If you've already got a database of blocked IPs, just query the database for a match on their $_SERVER['REMOTE_ADDRESS'] and exit if you find a match.
xsuck91
Forum Newbie
Posts: 2
Joined: Fri Oct 21, 2011 9:59 am

Re: Block IP with Database

Post by xsuck91 »

Celauran wrote:If you've already got a database of blocked IPs, just query the database for a match on their $_SERVER['REMOTE_ADDRESS'] and exit if you find a match.
can you give me a code ?

i dont have the database but i want to make it first so every visitor come to my site cannot see my site for twice time with the same IP
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Block IP with Database

Post by mikeashfield »

You'll be better using a cookie.

When the user first accesses the site, you store the cookie, when they come back, you check for the presence of that cookie, if it's there, you exit. Using a database of IP addresses would be impractical. IP addresses are typically dynamically assigned to end users, so it's entirely possible that you could inadvertently block access to your site to users that haven't used your site yet.


Mike.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Block IP with Database

Post by Celauran »

The problem with using cookies is that they can easily be deleted by the user, rendering the whole exercise pointless. While you're right that most users will have dynamic IP addresses, most broadband providers -- as far as I know, at any rate -- provide one IP to one client for months at a time.
martelo59
Forum Newbie
Posts: 6
Joined: Sat Oct 22, 2011 3:55 am

Re: Block IP with Database

Post by martelo59 »

First time script will show your data or other action, just put in { } when other (2.,3.,4., times) go to page will be automaticly redirected.

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];

if(!file_exists("$ip.txt"))
{
echo "YOUR first DATA SHOW";
}
else
{
echo '<script language="javascript">location.replace("other.php");</script>';
}

$upis_otvaranje=fopen("$ip.txt","w+");
fwrite($upis_otvaranje,"");
fclose($upis_otvaranje);
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Block IP with Database

Post by egg82 »

already posted this code once, but it seems relevant, so:
This will get a user's IP even if they're behind a proxy. Works so far, not sure about multiple proxies.

Code: Select all

$real_ip = $_SERVER['REMOTE_ADDR'];
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
	if (array_key_exists($key, $_SERVER) === true) {
		foreach (explode(',', $_SERVER[$key]) as $ip) {
			if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
				$real_ip = $ip;
			}
		}
	}
}
and query the database:

Code: Select all

$result = mysql_query("SELECT * FROM `ips` WHERE `ip`='".$real_ip."';");
if(!$result){
	echo(mysql_error());
	exit();
}
if(mysql_num_rows($result) == 0){
	echo("OK");
}else{
	echo("Your IP has been blocked");
}
Post Reply