Web Programming  Home Web Programming PHP IP Banning
rss

IP Banning

Author: Kyscorp.com More by this author


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 !



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "IP Banning"