PDA

View Full Version : [PHP] IP Banning Guide



Buax
04-03-2008, 10:53 PM
I know it's basic, but it's useful for new site owners or people that want to ban people! This is great if you are getting abuse/spam though a contact-box.

This is where you'll list all of the IP's you'd like banned. Please do not ban these IP addresses, they are you're own in case you didn't know. We are separating the IP addresses using a '|' symbol. We'll be splitting them up using this symbol in a minute!

<?php
// The ip addresses to be banned separated by |
$ban = "127.0.0.1|127.0.0.2|127.0.0.3");
?>

This part of the script will store the current users IP address in a varible to save coding space. $visitor will now be the users IP.

<?php
// Store current user's IP in a variable
$visitor = $_SERVER['REMOTE_ADDR'];
?>

This part of the script will split up the IP addresses using the '|' symbol

<?php
// Split the IPs using |
$list = explode("|", $ban);
?>

The final part of the script will check the current IP address of the user ($visitor) with the list of IP's which have just been split up.

<?php
foreach($list as $ip)
{
if($visitor == $ip)
{
echo "You have been banned from this website!";
exit;
}
}
?>

If you are too lazy to put it all together. It will look like this.

<?php

// The ip addresses to be banned separated by |
$ban = "34.43.97.31|86.53.73.27|41.107.14.621");

// Store current user's IP in a variable
$visitor = $_SERVER['REMOTE_ADDR'];

// Split the IPs using |
$list = explode("|", $ban);

foreach($list as $ip)
{
if($visitor == $ip)
{
echo "You are banned from this site";
exit;
}
}

?>

Now that is quite a bit of code to put on every single page. If you use PHP includes, we can cut it down to three lines, although you have to create a PHP file on you're website.


<?php
include ('banned_users.php');
?>

Jme
04-03-2008, 11:44 PM
You could save some code and just do..


<?php
$banned = array("34.43.97.31", "86.53.73.27", "41.107.14.621");//Seperate each by a comma.
$uip = $_SERVER['REMOTE_ADDR'];//Visitors IP Address.
if(in_array($uip, $banned))//Searches the array for the users IP.
{
die "You are banned..";//They're banned
}
?>
but nice tutorial none the less.

Buax
04-03-2008, 11:54 PM
You could save some code and just do..


<?php
$banned = array("34.43.97.31", "86.53.73.27", "41.107.14.621");//Seperate each by a comma.
$uip = $_SERVER['REMOTE_ADDR'];//Visitors IP Address.
if(in_array($uip, $banned))//Searches the array for the users IP.
{
die "You are banned..";//They're banned
}
?>
but nice tutorial none the less.
That would of been the easy way todo it.

Slowpoke
04-03-2008, 11:59 PM
Very useful, should be stuck, as should your other guides :).

Jme
05-03-2008, 12:11 AM
That would of been the easy way todo it.
Exactly my point. It does the exact same with less lines of code, why have extra more complex code when there's a simpler way that is just as good?

Unpredictible.1
05-03-2008, 12:14 AM
I just laughed at the ownage..

If you was good at PHP you would of known how to do it properly like Jamie said.

Buax
05-03-2008, 12:45 AM
I just laughed at the ownage..

If you was good at PHP you would of known how to do it properly like Jamie said.

The way Jamie did it was more advance then my method, I'm bad at PHP, therefore I used a easy method - His is shorter etc, does the same task, therefore better, although it's more advance. Yeah coding a like his would of been the easier way to do but I used the way I thought was easy for me.

Sorry for trying to post scripts in the community? :|

Pyroka
05-03-2008, 12:52 AM
Very useful +rep I might just use this in my serverside course.

Hypertext
05-03-2008, 12:56 AM
Very useful +rep I might just use this in my serverside course.

Since when could you code anything other than freewebs :P:P:P:P:P

Pyroka
05-03-2008, 01:00 AM
OI! I'm actually pretty good I'll have you know. I can do CSS, the javascript C+P method & advanced HTML along with a bit of XHTML and XML. I'm so amazing.

Hypertext
05-03-2008, 01:02 AM
lol :)

Edited by H0BJ0B (Forum Moderator): Please do not post pointlessly.

Jme
05-03-2008, 10:54 AM
Dude relax man, it's still a good tutorial i was just demonstrating an easyer way :]

Masterfood680
06-03-2008, 12:23 AM
Nice tut, but what about Dynamic IP's and proxies?

Buax
06-03-2008, 12:41 AM
Dude relax man, it's still a good tutorial i was just demonstrating an easyer way :]
Sorry - Some kid complained because I didn't have any evidance I knew some PHP, then it's not amazing, I know, I couldn't explain how I did it, it was easier for myself then explaining/coding you're way. :/.


Nice tut, but what about Dynamic IP's and proxies?
You can't do much about this just keep on banning them as they come.

Pyroka
06-03-2008, 12:42 AM
Yeah, if you found out their base IP then you could ban them fully, but that's pretty hard when you think about it. I remember there was a VBUlletin tool whcih could do that, quite interesting actually.

Nice tut, <33

Mentor
19-03-2008, 07:10 PM
OI! I'm actually pretty good I'll have you know. I can do CSS, the javascript C+P method & advanced HTML along with a bit of XHTML and XML. I'm so amazing.
I have no idea what the C+P javascript method is, nore do i have any recollection of Advance html, would this be the same thing as JUST html?

matt1234
19-03-2008, 07:20 PM
nice tut for people who didn't know it

Independent
24-03-2008, 08:06 AM
Why would you want to IP Ban someone anyway... It's not like they can't just bypass it by changing there IP or using a proxy..

Nearly everyone who uses the internet knows how to use proxy's/change there IP..

Meti
25-03-2008, 01:15 PM
Nice tutorial, but this could be found on DynamicDrive.com

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