PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP IP Banning

IP Banning


IP BanningIntroduction

Like many webmasters, you're bound to attract some annoying visitors, such as spammers. This is inevitable, but you can fight back. The simplest method of cyber combat is banning the IP, although this is not the most powerful method it works 90% of the time. Continue reading to figure out how this process works, or if you’re a codeshark scroll down to the bottom of the page.

Overview

With an already gathered IP address, we're going to ban an annoying visitor! We’re going to be using arrays and loops in this process.

Step by Step Tutorial....

This tutorial requires you to have the IP address of the visitor, so I assume you already know how to gather the IP. To be sure, I’ll post the code:

<?php $ip = $_SERVER['REMOTE_ADDR'];

Now, let’s finish off the rest of the variables, which will be in array format and an extension of the ray. Notice the code posted below. You can add more IP addresses in the array by separating them with a comma and start the semi quotations. The count function will count the variables in the array.

$ban = array('333.333.333.333',’111.111.111.111’);
$count = count($ban);

After that, we need to start the loop which will require the count function. If you don’t know how to use loops I suggest you read up on them.

for ($i=0; $i<$count; $i++) {
if($ip == $ban[$i]) { die("I'm sorry, you've been banned. $ip"); } } ?>

Final product

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ban = array('333.333.333.333','68.225.34.86');
$count = count($ban);
for ($i=0; $i<$count; $i++) {
if($ip == $ban[$i]) { die("I'm sorry, you've been banned. $ip"); }
}
?>

Enjoy !



Author's URL: Kyscorp.com
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments DenisovMIHAIL30 February 03, 2012 says:
???????? <a href="">??????? ? ?????</a> ?? ????? ???? ? ????
Reply
comments Rawaz December 13, 2011 says:
That's quite good actually, but i suggest if you use database, it will be much better.
Reply
comments RT June 30, 2011 says:
Very effective. Not 100% as indicated, but could run off less computer saavy people.
Reply
comments moko June 11, 2011 says:
Hi,
I'd like to know if is possible to ban all the users from a specific country (italy, i.e.) using this method.
Thank's.

--
moko

Reply
Add comments to "IP Banning"

Captcha