PDA

View Full Version : [Tut] Super Simple php IP banning



Mentor
01-08-2006, 04:54 PM
IP Banning is a method often used to stop annoying or abusive users accessing a website system such as forums or shoutbox's.

A simple way to ban users by there Ips is to use a script like this>


<?php

//Array of Banned ips
$banned_ips[0] = "000.000.000.001";
$banned_ips[1] = "000.000.000.002";
$banned_ips[2] = "000.000.000.003";
$banned_ips[3] = "000.000.000.004";
$banned_ips[4] = "000.000.000.005";
$banned_ips[5] = "000.000.000.006";

// Loop Threw All the Ips.
for($K = 0; $K<sizeof($banned_ips); $K++)
{
//If an Ip matches the user ip. Stop the page from loading.
if($banned_ips[$K] == $_SERVER['REMOTE_ADDR']){
die("You have been Banned from viewing this Page.");
}
}
?>

What the above script does is use a hard coded array of Ip address's then match them against the user viewings IP address. If they match it then displays the "You have been banned from viewing this Page." message to that user, and does not display the rest of the page.
If the viewers ip is not matched, then they are allowed to view the page as they normally would.

The script needs to be placed at the top of a page to work correctly.

The Message shown to banned users can easily be edited, as can the lists of Banned Ips. If you wish to ban more than 6 ips, just add new entrys to the array following the sequence, aka the next one would be $banned_ips[6] = some ip address.
The rest of the script does require any changes.

Correctable (Forum Moderator) - Thread moved to Website Tutorials. Nice Tut

---MAD---
01-08-2006, 08:08 PM
To be honest these days, IP banning isnt as effective as it used to be. I think people can change their ip too easily now-a-days. But nice script non the less as people do still ban IPs :).

Sygon.
01-08-2006, 08:18 PM
Also theres the problem of ISP's going behind a proxy like ntl, you ban one ntl user you ban em all.

Mentor
01-08-2006, 08:19 PM
To be honest these days, IP banning isnt as effective as it used to be. I think people can change their ip too easily now-a-days. But nice script non the less as people do still ban IPs :).
still the most effective, changeing an ip is alot more work than deleiting a cookie "/ or destroyin a sesssion

Eric30
02-08-2006, 03:57 PM
$_SERVER['REMOTE_ADDR'] will show the IP of the connection to the server, with NTL it will show an NTL server.

To find the IP of a ntl user you need to use $_SERVER["HTTP_X_FORWARDED_FOR"]
But that wont work for people connected directly

Jae
03-08-2006, 03:42 PM
Well you could do, maybe i think i'm ****** at PHP.But couldnt you do like "if" for NTL and "else if" for others, maybe i dunno.

Mentor
04-08-2006, 12:57 AM
$_SERVER['REMOTE_ADDR'] will show the IP of the connection to the server, with NTL it will show an NTL server.

To find the IP of a ntl user you need to use $_SERVER["HTTP_X_FORWARDED_FOR"]
But that wont work for people connected directly
Thats just a very simple basic tutorial, its quite easy to do though



$ip = "";
if ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) && (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif ((isset($_SERVER['HTTP_CLIENT_IP'])) && (!empty($_SERVER['HTTP_CLIENT_IP'])))
{
$ip = explode(".",$_SERVER['HTTP_CLIENT_IP']);
$ip = $ip[3].".".$ip[2].".".$ip[1].".".$ip[0];
}
elseif ((!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) || (empty($_SERVER['HTTP_X_FORWARDED_FOR'])))
{
if ((!isset($_SERVER['HTTP_CLIENT_IP'])) && (empty($_SERVER['HTTP_CLIENT_IP'])))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
}
else
{
$ip = "0.0.0.0";
}

That should get the Ip from pretty much anything. you can then use the $ip varible when u need it :)

Jae
04-08-2006, 10:04 PM
I have been thinking, if you wanted to ban someone from all of your pages wouldn't you use


<?PHP
include('ban.php')
?>

Mentor
04-08-2006, 11:04 PM
if the script was in ban.php "/

nets
28-08-2006, 12:40 AM
<?php

$b[] = '0.0.0.1';
$b[] = '0.0.0.2';
$b[] = '0.0.0.3';

in_array($_SERVER['REMOTE_ADDR'], $b) &&die('You\'re banned.');

?>
Haven't tested.

Mentor
30-08-2006, 05:09 PM
humm the in array is probably a better way of doing it, although i think your syntax has problems



<?php

$b[] = '0.0.0.1';
$b[] = '0.0.0.2';
$b[] = '0.0.0.3';

if(in_array($_SERVER['REMOTE_ADDR'], $b)){
die("You're banned.");
}
?>

nets
31-08-2006, 07:58 PM
I think my syntax is fine.

Edit: http://joshjh.pwp.blueyonder.co.uk/testing.gif (PHP/5.1.2)

lolwut
02-09-2006, 07:20 PM
This is how I'd do it.

Save this as Ban.php if you want.


<?php
//Ban.php
$ip1 = 'xxx.xxx.xxx.xxx';

$ip2 = 'xxx.xxx.xxx.xxx';

$ip3 = 'xxx.xxx.xxx.xxx';


if($_SERVER['HTTP_X_FORWARDED_FOR'] == $ip1||$_SERVER['HTTP_X_FORWARDED_FOR'] == $ip2||$_SERVER['HTTP_X_FORWARDED_FOR'] == $ip3) {
echo("Sorry your banned.");
}
?>


Then on your index page, to check if the IP is banned... Add this code anywhere, preferably at the top.



<?php
include("ban.php");
?>

Mentor
05-09-2006, 10:16 PM
Thats actualy quite a good idea? Since you dont need to worry about whether the public or proxie Ips are not the one you banned since it checks all "/
Bravo

Eric30
06-09-2006, 10:12 PM
Nice tut there :)

Agnostic Bear
22-01-2007, 11:19 AM
Meh, you could use another way of doing it If you have a MySQL database and not alot of tables you can make)


<?php
include("configurationofmysqldatabasewithareallylongunneede dname.php");
$dynamite = mysql_fetch_array(mysql_query("SELECT `ip` FROM `bannageness` ORDER BY `ip` DESC"));
$explosion = explode(";", $dynamite["ip"]);
for($num = 0; $num < count($explosion); $num++)
{
if($_SERVER["REMOTE_ADDR"] == "$explosion[$num]")
{
die("lol banned");
}
}
?>

Bonxy
10-05-2008, 02:32 PM
right, i have this problem

my ban.php is in a directory such as

1/2/3/4/5/6/7/8/9/ban.php


and if i put this incluede on my homepage

<?php
include("ban.php");
?>

it only reads the same directory as the files in. and when i try to put in the full url such as

http://www.bonxy.co.uk/1/2/3/4/5/6/7/8/9/ban.php it comes up with loads of error lines. anyone know how i can get around this problem ????


+REP TO HELPERS!!!!!!


xxxxxx

DeejayMachoo$
10-05-2008, 08:17 PM
Why not also set a banned cookie and a session and make it check for that so if it has either of the 3 then your banned...?

Florx
11-05-2008, 05:24 PM
Can clear cookies, sessions, and change my IP very easily.

I don't really think there is a decent way of banning someone!

Want to hide these adverts? Register an account for free!