Top

Ban IP


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  1.7 (of 3 scores)
Date Added  Jul 26, 2007
Last Updated  Jul 26, 2007
Tags  ban 

Introduction

This is a short tutorial on how to ban an IP address from your website.

Either create a new .php document in a text editor or add the following php to an existing .php document. (ex. index.php)
--
Now to explain the code.

1. <?php - Starts the php tag. Lets the browser know what language you are using.
2. $ip = getenv('REMOTE_ADDR'); - Gets the users IP address
3. $blocked = "xx.xx.xx.xx"; - Tells the browser that the "xx.xx.xx.xx" IP is blocked/banned
4. if (ereg($blocked,$ip)) - If the blocked/banned IP is the same as the users IP, the following echo will be displayed.
5. { - Starts a bracket
6. echo "You Have Been Banned"; - Echos the "You Have Been Banned" line onto the page.
7. exit(); - Exit so no more content is ouput
8. } - Ends a bracket
9. ?> - Ends the php tag



Grab the Code

<?php
$ip = getenv('REMOTE_ADDR');
$blocked = "xx.xx.xx.xx"; // Replace the x's with the IP address.
 
if (ereg($blocked,$ip))
{
echo "You Have Been Banned";
exit();
}
?>
 
'echo "You Have Been Banned";'

Comments

  (8)  RSS
Hawkee
Comments: 1,039
 
PHP Snippet:  Ban IP
Posted on Jul 26, 2007 5:25 pm
Don't you mean "You haven't been banned" after the if-statement and exit?
[M]ike
Comments: 33
 
PHP Snippet:  Ban IP
Posted on Jul 26, 2007 5:31 pm
And if you do mean "You haven't been banned" - is that really neccisary? Surely not that many people are going to be banned, therefore it's easier to say nothing if they aren't. :)
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Ban IP
Posted on Jul 27, 2007 3:18 am
and also missing the else { (Dunno if it is necessary but i find it easier to code with) and why not use die("You are banned");
Snipe
Comments: 7
 
PHP Snippet:  Ban IP
Posted on Aug 10, 2007 3:16 pm
You should make an array so you can block multiple IP's
peterpowell
Comments: 39
 
PHP Snippet:  Ban IP
Posted on Aug 16, 2007 9:48 am
i made a script like this - it checked the ip against the ip adresses in ban.txt

try that ;)
alanhogan
Comments: 5
 
PHP Snippet:  Ban IP
Posted on Jan 8, 2008 4:17 pm
This script is incorrect... Won't it ban you if your IP is "89.12.34.20" and it's trying to ban "189.12.34.206"? Why not use == instead of ereg? It'll be faster anyway. This isn't Java; == doesn't compare identity in PHP but rather value!
alanhogan
Comments: 5
 
PHP Snippet:  Ban IP
Posted on Jan 8, 2008 4:21 pm
To be more constructive -- first off thanks for commenting your code and explaining it to n00bs. Secondly, if you want to use regular expressions, the preg_ functions are supposed to be faster/better. Thirdly, try '/^34\.12\.233\.254/$' to match one address or '/^34\.12\.233\.(1|2)?[0-9]{1,2}/$' to match a range. ^ means "Beginning of string" and $ means "end" when they are used in the first and last positions ([^6] means any character except 6)
Joshuaxiong1
Comments: 127
 
PHP Snippet:  Ban IP
Posted on Mar 8, 2009 4:49 pm
nice script

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom