Ban IPs Using MySQL

6 posts Page 1 of 1
Contributors
User avatar
wrighty1986
VIP - Donator
VIP - Donator
Posts: 119
Joined: Sat Sep 12, 2009 11:55 am

Ban IPs Using MySQL
wrighty1986
First you must make a tables into your database:

tables.sql:
Code: Select all
CREATE TABLE `banned_ip` (
`id` INT( 25 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`ip` VARCHAR( 25 ) NOT NULL ,
`reason` TEXT NOT NULL
)
connect.php
Code: Select all
$con = mysql_connect("localhost", "test", "test");
if(!$con){
die("Can not connect to database: ".mysql_error());
}
$db = mysql_select_db("test", $con);
if(!$db){
die("Can not select database: ".mysql_error());
}
Now admin panel

admin.php
Code: Select all
<?php
include("connect.php");

//This function will display the banning form
function ban(){

echo "<form action='?act=ban' method='post'>"
    ."<table width='500' border='1'>
    <tr>
    <td><center><font size='+2'>Ban!</font></center></td>
    </tr>
    <tr>
    <td>IP:<br> <input type='text' name='ip' size='30' /><br />
    Reason:<br />
    <textarea rows='10' cols='55' name='reason'></textarea><br>
    <input type='submit' value='Ban!'>
    </td>
    </tr>
    </table>";
    
echo "<br><br>";

//Let's connect to database and choose the database
$result = mysql_query("SELECT * FROM banned_ip");
$rows = mysql_num_rows($result);
if($rows > 0){

//Display banned IPs 
echo "<table width='700' border='1'>
    <tr>
    <td><center><font size='+2'>Banned IPs</font></center></td>
    </tr>
    <tr>
    <td>";
        echo "<table border='1' width='100%'>"
            ."<tr>"
            ."<td width='25%'><b>Id</b></td><td width='25%'><b>Ip</b></td><td width='25%'><b>Reason</b></td><td width='25%'><b>Action</b></td>"
            ."</tr>";

while($row = mysql_fetch_array($result)){
echo "<tr><td width='25%'>".$row['id']."</td><td width='25%'>".$row['ip']."</td><td width='25%'>".$row['reason']."</td><td width='25%'><a href='?act=del&id=".$row['id']."'>Delete</a></td></tr>";
}

    echo "</table>";
    
echo "</td>
    </tr>
    </table>";
}
}

//This function will ban a IP
function ban_ip(){

$ip = $_REQUEST['ip'];
$reason = $_REQUEST['reason'];

//If there are errors display them in here
if(empty($ip)){
die("Please insert a IP!");
}

if(empty($reason)){
die("Please insert a reason!");
}

//Let's insert this ip and reason into your database

$insert = mysql_query("INSERT INTO banned_ip (ip, reason) VALUES ('$ip', '$reason')");

//If there was problems with inserting those things into database, lets display the error
if(!$insert){
die(mysql_error());
}

echo "<table width='500' border='1'>"
    ."<tr>"
    ."<td><center>Info</center></td>"
    ."</tr>"
    ."<tr>"
    ."<td><br>IP: <b>$ip</b> was banned, Reason: <b>$reason</b><br><a href='admin.php'>Back</a><br></td>"
    ."</tr>"
    ."</table>";
}

//This function will delete banned ip

function del(){

$id = $_GET['id'];

$del = mysql_query("DELETE FROM banned_ip WHERE id='$id'");

//If there's problem with deleting then display an error
if(!$del){
die(mysql_error());
}

echo "<table width='500' border='1'>"
    ."<tr>"
    ."<td><center>Info</center></td>"
    ."</tr>"
    ."<tr>"
    ."<td><br>Ban deleted, <a href='admin.php'>Go back</a><br></td>"
    ."</tr>"
    ."</table>";

}

switch($act){

default;
ban();
break;

case "ban";
ban_ip();
break;

case "del";
del();
break;

}

?>
Well now make another file called

bans.php
Code: Select all
<?php
include("connect.php");
$ip = $_SERVER['REMOTE_ADDR'];
$find_ip = mysql_query("SELECT * FROM banned_ip WHERE ip='$ip'");
$ban = mysql_fetch_array($find_ip);
if($ip == $ban['ip']){ die("You are banned from this site!");}
?>
just add this code to your files and your stuff is protected
Code: Select all
<?php
include("bans.php");
?>
and your files are protected.. wahooo;
Back Aegean sorry not been on i.v just been moving.
Lewis
Coding God
Coding God
Posts: 1564
Joined: Sun Dec 20, 2009 2:12 pm

Re: Ban IPs Using MySQL
Lewis
This is a great tutorial - Thanks! could you make it so it shows them the reason?
Image
User avatar
wrighty1986
VIP - Donator
VIP - Donator
Posts: 119
Joined: Sat Sep 12, 2009 11:55 am

Re: Ban IPs Using MySQL
wrighty1986
ye thers ways mate just add a table to the db putin the info on the php file mate
Back Aegean sorry not been on i.v just been moving.
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

Re: Ban IPs Using MySQL
zachman61
awesome job Wrighty
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
NoWayIn
VIP - Donator
VIP - Donator
Posts: 444
Joined: Sat Nov 21, 2009 11:16 pm

Re: Ban IPs Using MySQL
NoWayIn
Good tutorial !
The think I don't like about IP is that it can be easily changed, no matter if you have static or dynamic IP, you can change it =\
User avatar
wrighty1986
VIP - Donator
VIP - Donator
Posts: 119
Joined: Sat Sep 12, 2009 11:55 am

Re: Ban IPs Using MySQL
wrighty1986
Ye thats wot i dont like to but thers loads of ways do get past it haha.
Back Aegean sorry not been on i.v just been moving.
6 posts Page 1 of 1
Return to “Help & Support”