| NAME: |
|
Ban IP Addresses from your site |
| AUTHOR: |
|
punkstar |
| DATE: |
|
27th Nov, 2005 - 01:33:19 PM |
| DESCRIPTION: |
|
How to make an easy an maintainable IP blocking PHP system (updated: 20/01/07) |
Need some help with that coding?
Okay, the basic scenario is this. There is a really annoying person on the forum that threatens to hack and take down your site (yadda yadda yadda), so for some reason.. for this, we want to ban him from the site.
We are going to have a MySQL table from which you will enter the IP Address of the banee. Then there will be a place to enter a reason.
So lets create the table..
| Code | CREATE TABLE `banned` (
`ip_addr` varchar(15) NOT NULL default '',
`reason` varchar(255) NOT NULL default ''
) |
Then simply log into phpMyAdmin or something, so that you can add your records easily, and enter the ip address of your banee, and a reason to show him/her when they get the bump.
Now at the top of every page you want them banned from, throw this code.
| PHP Code |
<?php
//remember to put your MySQL information here, check out the php tutorial section if you dont know how to do this.
$sql = "SELECT `reason` FROM `banned` WHERE `ip_addr` = '".$_SERVER['REMOTE_ADDR']."'";
$dosql = mysql_connect($sql,$connect);
if(mysql_num_rows($dosql) == 1)
{
//the user is banned, lets show him the reason.,
$reason = mysql_fetch_assoc($dosql);
echo "You have been banned, loser.<br /><br />";
echo "Reason: ".$reason[0];
exit;
}
?>
|
The code is pretty easy. The exit(); makes the php script stop completely and not output anything else, or carry out any other operation on that page. It essentially tells the php script that there is nothing left to do, so it quits out.
Then you can try making your own admin area for this, so that you can easily add your own IP bans etc. You could even try adding timed bans ;)
|