ManyCodes.com – codes & scripts Get free programming codes and tutorials!

17Sep/101

Ban IP using PHP

This piece of code will allow a webmaster to be able to ban a person if you know their IP.  Basically the webmaster can record the ip of the user and prohibit them to come to your website and visit your webpage.

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

<pre><!-- This Script is from www.hawkee.com, found at www.manycodes.com-->
<?php
$ip = getenv('REMOTE_ADDR');
$blocked = "xx.xx.xx.xx";
if (ereg($blocked,$ip)) {

echo "You Have Been Banned";
exit();
}
?>
<br><font face="Tahoma"><a target="_blank" href="http://www.manycodes.com/category/php/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font>

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 rowser 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

Tagged as: , , 1 Comment