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

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
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "IP Banning"

Only registered users can write comment

Reader's comments