IP Banning System

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
Learn how to IP ban people Admin Section Included

Ok so first we need to do some things like creat a table for the ip's to be sent to. Use the flowing code

Code:
CREATE TABLE `ip` (
`id` BIGINT( 25 ) NOT NULL AUTO_INCREMENT ,
`ip` VARCHAR( 25 ) NOT NULL ,
`banned` ENUM( 'yes', 'no' ) DEFAULT 'no' NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM ;

What that code does is create a table to store the IP addresses. You can modify the code if needed.
------------------------

Now we need to create a database connect page. It should look like the following:

name it db.php
PHP:
<?
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
$dbhost = '*****'; //Usually localhost
$dbusername = '*******';
$dbpasswd = '*******';
$database_name = '*******';

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
	or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");
?>

Basically what that does is create a connection to the database.
------------------------

Next we need to create the IP page. That page should look like this:
name this ip.php
PHP:
<?php
include ('db.php');
$ip = $_SERVER['REMOTE_ADDR'];

$queryip = mysql_query("SELECT * FROM `ip` WHERE `ip` = '$ip'") or die (mysql_error());

$rows = mysql_num_rows($queryip);

if ($rows > 0) {
}else{

$insertip = mysql_query("INSERT INTO `ip` (`ip`, `banned`) VALUES ('$ip','no')") or die 
(mysql_error());

}

$row = mysql_fetch_array($queryip);
$banned=$row['banned'];

if (($banned)=='yes'){
echo "You were banned from this site.";
exit();
}else{
}
?>

What that code does above is get their IP and check if they were banned or not.
------------------------
Admin Panel Section
------------------------
Ok lets make the admin section of the IP banner

This page will let you edit the ips that need to be banned. It will also allow you to ban many at once.

name this page ipban.php
PHP:
<?php
ob_start();

include ('db.php');
$result=mysql_query("SELECT * FROM `ip`");

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="400%" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>IP</strong></td>
<td align="center"><strong>Banned</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="ip[]" type="text" id="ip" value="<? echo $rows['ip']; ?>"></td>
<td align="center">Yes or No<input name="banned[]" type="text" id="banned" value="<? echo $rows['banned']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){

$banned[$i] = strtolower($banned[$i]);
$sql1="UPDATE `ip` SET ip='$ip[$i]', banned='$banned[$i]'WHERE id='$id[$i]'";
$result1=mysql_query($sql1);

}
}

if($result1){
header("location:ipban.php");
}
mysql_close();
ob_end_flush();
?>

------------------------
That is a simple banning script. I will post an update to this script making it so you can add reasons to why they were banned.


Note: This script may not be shared with out my consent. Umm. all that other legal mumbo gumbo here (c) Chris Sterling
 
M

minievan

Guest
Thanks for that, i think i have seen a smaller/easier one before. Good anyway ;)
 

Jordan K

[B]tags dont work:([/B]
Messages
1,528
Reaction score
0
Points
0
Excellent share, this could be very useful for those malacious looking to cause some havoc. Thanks I might use this myself and thanks to you for everyone else out there who uses it.
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
and if you are on a server that doesn't support cpanel? but thanks for the comments
 
B

Brandon

Guest
Sorry didn't think of that.
ughdance.gif
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
no problem. not everybody does have cpanel, and not all people can afford cpanel. but yes that is an easier way to ban IP's
 

Progamers

New Member
Messages
32
Reaction score
0
Points
0
Can't you use .htaccess to ban ips? Theres a tutorial on google.com. You can ban them by directory this way.
 

Cynical

Active Member
Messages
3,492
Reaction score
0
Points
36
I might like to add that you might want to password protect your admin panel for something like this, or at least give it some not-so-obvious filename, otherwise you might have people banning random IPs from your site :kdubb: .
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
well thats up to the user. I have no real idea on how they will implement to their site.
 

flapietoetoe

New Member
Messages
226
Reaction score
0
Points
0
u could just make a function :
$_SERVER['REMOTE_HOST']
check if the ip is in the database
if(countrows($databasequery)==0)
{show content
}
else redirect it to a banned page...
but still nice script m8

and u could bypass it with a proxy ;)
 
Top