NameSilo

[PHP] IP Banner

Spaceship Spaceship
Watch
Impact
0
If you are looking to ban a few IPs, try this.

Code:
if (strcasecmp(getenv("REMOTE_ADDR"), "someIP") == 0)
{
  echo "You have been banned.\n";
  exit;
}
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Hmm, nice! But you'll have to have that for every single IP and you can't ban an IP rande in 1 line.
 
0
•••
Well yea, it's better through cpanel though. But I think this is good for banning a few IPs. :)
 
0
•••
You could also use strstr

Code:
if (strstr(getenv("REMOTE_ADDR"), "1.2.3.4") != FALSE)
{
  banned
}
 
0
•••
Possibly this would work better: Allows the ban of multiple IPs, one per line.

PHP:
//Place this at the top of your php scripts/HTML to prevent access to those IP addresses listed.
<?php
$ip_ban = array();
$ip_ban[] = '111.111.111.111';
$ip_ban[] = '111.111.111.112';

foreach($ip_ban as $denied) {
	$ip = $_SERVER['REMOTE_ADDR'];
	if($ip == $denied){
		echo "Access Denied! You have been banned!";
		exit();
	}
}

?>
 
Last edited:
0
•••
owow its very small. nice script.
 
0
•••
SecondVersion said:
Possibly this would work better: Allows the ban of multiple IPs, one per line.

PHP:
//Place this at the top of your php scripts/HTML to prevent access to those IP addresses listed.
<?php
$ip_ban = array();
$ip_ban[] = '111.111.111.111';
$ip_ban[] = '111.111.111.112';

foreach($ip_ban as $denied) {
	$ip = $_SERVER['REMOTE_ADDR'];
	if($ip == $denied){
		echo "Access Denied! You have been banned!";
		exit();
	}
}

?>

You would be as well doing the following

PHP:
<?php
$ip_ban = array();
$ip_ban[] = '111.111.111.111';
$ip_ban[] = '111.111.111.112'; 

if (in_array($_SERVER['REMOTE_ADDR'], $ip_ban)) {
   echo "Access Denied! You have been banned!";
   exit();
}
?>
 
0
•••
If I was to use them / this, I would use the array, and put it into like banned.php and then just call / include banned.php on the footer which is on every page or somthing...

PJ
 
0
•••
tis a cool script, but I can never figure out how this kinda script works.

Firstly, with my service provider, my ip address changes every 5 minutes, so I can just wait a few minutes before re-accessing the site, and other people's ip addresses re-knew when they re-logon to their isps. These won't work, I'm telling you! The rest of the people will just use an ipscrambler.
 
0
•••
yes that is a big problem for using ip's to ban a user.
 
0
•••
Cookies are a kind-of-good way to handle that.

If the visitor blocks cookies, you're out of luck...

I'd recommend setting them with a misleading name, so Firefox users don't think they say you're banned! (i.e. site_user). When the user visits the site, add the site_user cookie with no_c. If they're banned, add it with no_b.

Misleading titles of cookies should work!
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back