How to 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.

MySQL:
  1. CREATE TABLE `bannedip` (
  2. `ip` VARCHAR(255) NOT NULL,
  3. PRIMARY KEY  (`ip`),
  4. UNIQUE KEY `ip` (`ip`)
  5. ) ENGINE=INNODB DEFAULT CHARSET=latin1;

The Form for banning

HTML:
  1. Please enter the IP address you wish to ban:
  2.  
  3. <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:
  1. mysql_connect("localhost", "root", "") or die(mysql_error()); //connect to db
  2. $ip = $_POST['ip']; //get IP address
  3. if(!empty($ip)){ // check it has been entered
  4. mysql_query("INSERT INTO bannedip (ip) VALUES ('$ip')") or die(mysql_error()); //insert into table or give error
  5. echo "<strong>IP Banned</strong>
  6. ";//show success message
  7. }else{
  8. echo "Error, Please go back and fix it.";//otherwise show error message
  9. }
  10. ?&gt;

That was easy. Now you want to display a message to them. So we make a file named banmessage.php

PHP:
  1. mysql_connect("localhost", "root", "") or die(mysql_error());
  2. mysql_select_db("dbname") or die(mysql_error());//connect to db
  3. $ip = $_SERVER['REMOTE_ADDR'];//get users IP address
  4. $query = mysql_query("select * from bannedip where ip='$ip'");// see it it exists
  5. $countbans = mysql_num_rows($query); //check its in the db
  6. if($countbans&gt; 0) {
  7. die("You have been banned By The Administrator!");//show message in a "die"
  8. }
  9. ?&gt;

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

HTML:
  1. Please enter the IP address you wish to unban:
  2.  
  3. <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:
  1. mysql_connect("localhost", "root", "") or die(mysql_error());
  2. mysql_select_db("dbname") or die(mysql_error()); //connect to db
  3. $ip = $_POST['ip']; //get posted IP
  4. if(!empty($ip)){//check something has been sent
  5. mysql_query("DELETE FROM bannedip WHERE `ip` = '$ip'") or die(mysql_error()); //delete ip sent or give error
  6. echo "<strong>IP Allowed Access AGAIN</strong>
  7. ";//show success message
  8. }else{
  9. echo "Error, Please go back and fix it."; //show error message
  10. }
  11. ?&gt;

I hope you have found it easy to follow and find it useful.

VN:F [1.9.3_1094]
Rating: 8.5/10 (2 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
How to ban somebody who has done something wrong on your site, 8.5 out of 10 based on 2 ratings
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

1 Comment

  1. Too bad they could be behind a proxy, or dial-up. I suggest using the XIP Class which can be found on phpclasses.org

    Still good though.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

Leave a Reply