[PHP] IP Banner

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Watch

i386

Established Member
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
Well yea, it's better through cpanel though. But I think this is good for banning a few IPs. :)
 
0
Close
You could also use strstr

Code:
if (strstr(getenv("REMOTE_ADDR"), "1.2.3.4") != FALSE)
{
  banned
}
 
0
Close
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
Close
owow its very small. nice script.
 
0
Close
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
Close
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
Close
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
Close
yes that is a big problem for using ip's to ban a user.
 
0
Close
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
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

New: watch any domain
You can now watch domains on NamePros. We check each one and notify you when something changes. Search for a domain and press Watch on...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back