PHP Basics: IP Banning Script

At some point or another you’re going to want to know how to do this. You’re in luck, as it’s pretty easy to do.

Reasons to ban an IP

You may have visitor logging and you notice some unusual behaviour, for safety, it’s a good idea to ban this individual, and you can even give them a message if you wish in your ban, that way they can explain themselves, or bugger off.

You could have been hacked, and you’ve retrieved your logs, got your site back up, and you want to ban the individuals. It’s a good idea to do so.

You just want to know how to do it, to impress your friends, they’ll think you’re so damn cool and you’ll ‘get the girl’.

Reason why IP Banning May not always Work

Ip banning may not work if the offender as a dynamic IP address, when they reset their router or modem, their ISP will give them a new IP address. I’m afraid there isn’t much you can do about this, but if they are a real menace, I suggest resolving the IP address to find the Internet Service Provider, and call the provider to report them. Most ISP’s will keep a log and will give a friendly warning to them to stop their shinanigans.

The PHP Script

<?php
$bannedipaddress[0]="xxx.xxx.xxx.xxx"; // IP LIKE "123.12.12.3" or whatever
$bannedipaddress[1]="xxx.xxx.xxx.xxx";  // you can add as many as you like to the PHP array

// add as many as you wish

if (in_array($_SERVER['REMOTE_ADDR'],$bannedipaddress)) header("HTTP/1.1 403 Forbidden");
// you can change the header location to any URL you like, that's where the banned user will go
?>

Tags: , , ,

Author:Alan Hamlyn

-- Alan Hamlyn Founder of Wuup
  • Anonymous

    HTTP/1.1 403 Forbidden”);
    // you can change the header location to any URL you like, that’s where the bann