The place that has your tutorials

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. <p>Please enter the IP address you wish to ban:</p>
  2. <form name="form1" method="post" action="banipproc.php">
  3.   <input name="ip" type="text" id="ip">
  4.   <input type="submit" name="Submit" value="Submit">
  5.   <input type="reset" name="Submit2" value="Reset">
  6. </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. <?php
  2. mysql_connect("localhost", "root", "") or die(mysql_error()); //connect to db
  3. mysql_select_db("dbname") or die(mysql_error());
  4. $ip = $_POST['ip']; //get IP address
  5. if(!empty($ip)){ // check it has been entered
  6. mysql_query("INSERT INTO bannedip (ip) VALUES ('$ip')") or die(mysql_error()); //insert into table or give error
  7. echo "<strong>IP Banned</strong><br>";//show success message
  8. }else{
  9. echo "Error, Please go back and fix it.";//otherwise show error message
  10. }
  11. ?>

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

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

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

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

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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

Leave a Reply