Amazon

Tuesday 25 October 2011

ban somebody who has done something wrong on your site

Well, first of all we need a form where you enter their IP address.
Firstly use the following SQL to create the table.
http://3.bp.blogspot.com/-ujGdQBt1rMM/TqeYgwqWJDI/AAAAAAAAAnk/FlPfxYun7l0/s1600/php.gif

MYSQL:
CREATE TABLE `bannedip` (
`ip` VARCHAR(255) NOT NULL,
PRIMARY KEY  (`ip`),
UNIQUE KEY `ip` (`ip`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;


The Form for banning:

HTML:
Please enter the IP address you wish to ban:
<form action="banipproc.php" method="post"> <input id="ip" name="ip" type="text" /> <input name="Submit" type="submit" value="Submit" /> <input name="Submit2" type="reset" value="Reset" /> </form> 


This will take you to a page called banipproc.php
There what we do is add the entered IP address into a table:
PHP:
mysql_connect("localhost", "root", "") or die(mysql_error()); //connect to db
mysql_select_db("dbname") or die(mysql_error());
$ip = $_POST['ip']; //get IP address
if(!empty($ip)){ // check it has been entered
mysql_query("INSERT INTO bannedip (ip) VALUES ('$ip')") or die(mysql_error()); //insert into table or give error
echo "<strong>IP Banned</strong>
";//show success message
}else{
echo "Error, Please go back and fix it.";//otherwise show error message
}
?>

That was easy. Now you want to display a message to them. So we make a file named banmessage.php
PHP:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());//connect to db
$ip = $_SERVER['REMOTE_ADDR'];//get users IP address
$query = mysql_query("select * from bannedip where ip='$ip'");// see it it exists
$countbans = mysql_num_rows($query); //check its in the db
if($countbans> 0) {
die("You have been banned By The Administrator!");//show message in a "die"
}
?> 


Now everyone has a sense of forgiveness and you might want to unban them.

 HTML:
Please enter the IP address you wish to unban:
<form action="unbanipproc.php" method="post"> <input id="ip" name="ip" type="text" /> <input name="Submit" type="submit" value="Submit" /> <input name="Submit2" type="reset" value="Reset" /> </form>


This will take you to a page called unbanipproc.php. In there all we do is remove the entry. Like so:
Now you also make a form very simmilar to the one above:

PHP:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error()); //connect to db
$ip = $_POST['ip']; //get posted IP
if(!empty($ip)){//check something has been sent
mysql_query("DELETE FROM bannedip WHERE `ip` = '$ip'") or die(mysql_error()); //delete ip sent or give error
echo "<strong>IP Allowed Access AGAIN</strong>
";//show success message
}else{
echo "Error, Please go back and fix it."; //show error message
}
?>


Thats it. please leave your coments.....