This tutorial will show you how to ban people by IP address with php and mysql. First you need to go into php my admin and create the follow tables:
CREATE TABLE banip (
ID int(12) NOT NULL auto_increment,
IP varchar(25) NOT NULL default '',
PRIMARY KEY (ID)
) TYPE=MyISAM;
This is the basic table structure with only two fields, a field for a ID that identifies a unique field and a field for all IP's banned. Now we'll look at the code that actually bans the IP on each. Structure each page you want IP banning this way.
<?php
include "connect.php";
//this is the file that connect to sql
$s=$_SERVER["REMOTE_ADDR"];
//draws IP address of visitor
$ipbancheck="SELECT * from banip where IP='$s'";
$ipbancheck2=mysql_query($ipbancheck);
while($ipbancheck3=mysql_fetch_array($ipbancheck2))
{
$IPBANNED=$ipbancheck3[IP];
}
//above lines check to see if user Ip is in banned IPs
if ($IPBANNED)
{
print "You have been banned ";
}
else
{
//put content you want unbanned users to see here
}
?>
Okay, this code should be fairly simple to understand. $_SERVER["REMOTE ADDR"] draws the IP address of the user
the next line select from the table of baned IP where a field matches the user's IP. If no fields match the users IP, it comes up blank.
The while loop sets the variable IPBANNED equal to the banned IP if the first select statement had any matches and, if the select didn't have any matches, it doesn't set a IPBANNED variable.
Then when the the next few lines says if the IPBANNED variable exists, then we shows the 'You have been banned" message, otherwise show the actual page content in the else case.
If you have any questions please post in the Support Forums
|