PHP MySQL ebook
February 12, 2011 – 4:57 pm | No Comment

Hey everyone!!
With this section I hope to share with you resources which I have tried and valued
So you’re new to PHP and want to learn from scratch. Well I was in your position not too …

Read the full story »
SEO Tutorials

Need help with Search Engine Optimisation? This is the place to look for the latest tips and hints with the advice we offer. Keep an eye out here for the latest posts.

PHP Tutorials

Learn how to code with PHP to achieve your goal. In here I aim to show and teach you about little tips and tricks that should enhance your coding abilities

Flash Tutorials

Learn how to use Adobe Flash for your own purposes. Little tips and hints to help you along

Website Development

In here I aim to help you with developing your website from scratch. Come here for various tips and pieces of advice to help you build and improve your website.

Home » Featured, PHP Tutorials

How to ban somebody who has done something wrong on your site

Submitted by on July 29, 2007 – 3:19 pmOne Comment

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.13_1145]
Rating: 9.0/10 (3 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
How to ban somebody who has done something wrong on your site, 9.0 out of 10 based on 3 ratings

Popularity: 14% [?]

One Comment »

  • ShadowMage says:

    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.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.