PHP Script – IP Banned
Tagged Under : block, block domain, block robots, Blocking unwanted robots, php
Version: 13.2
Revision: 39 Build 16
PHP Script – IP Banned
Introduction: last night, I was repairing my “HTA” files, it was a little messed up, so I have to re-write parts of the codes and scribble it down into smaller bits. However, it was boring as hell!
Here is my little “IP-Ban.php” script. I designed this script from scratch. But, I don’t know if I want to use it.
Don’t get me wrong…I did test the script, It works…
1.] Download notepad++ from the original author or from a mirror and install the software.
——————————-
http://sourceforge.net/projects/notepad-plus/
http://filehippo.com/download_notepad/
——————————-
2.] Copy this “PHP Script” and save it as a “ban-ip.php” extension.
3.] Select one of these “IP script” and replace the default IP address with your own address.
After that, just add this “include” string, straight into your wordpress files.
—Copy Source Code—
<?php
include("ip-ban.php");
?>
—End Source Code—
Special Notice: by following the rules and regulations, these scripts was created / written by me from scratch. They are also revised for stability and bugs free. However, please use these script at your own risk!
Method One – Blacklist Host Style
—Copy Source Code—
<?php
$blackList = array();
$blackList[] = "192.168.1.1";
$blackList[] = "192.168.2.*";
if(in_array($_SERVER['REMOTE_ADDR'], $blackList)) {
die("You have been banned!");
} else {
foreach($blackList as $blackIP) {
if(eregi($blackIP,$_SERVER['REMOTE_ADDR'])) {
die("You have been banned!"); }
}
} ?>
—End Source Code—
Method Two – Blacklist Simplified Style
—Copy Source Code—
<?php $banned = array (
//Add your own IP address under this line
'122.2.13.1',
'144.2.76.6',
'166.2.76.6',
// IP Address Ends Here
);
$ip = GetHostByName($REMOTE_ADDR);
if (in_array($ip,$banned)
{
echo 'You are banned from this site!';
exit();
} ?>
—End Source Code—
Method Three – Blacklist Intermediate Style
—Copy Source Code—
<?php
// IP in the form of "127.0.0.1" in a host form
$banned[0]="xxx.xxx.xxx.xxx";
$banned[1]="xxx.xxx.xxx.xxx";
$banned[2]="xxx.xxx.xxx.xxx";
// Ban IPs Ends
if (in_array($_SERVER['REMOTE_ADDR'],$banned))
{
header("location: http://www.google.com/");
exit();
} ?>
—End Source Code—
Method Four – Blacklist Text File
Notes: make a text file in the same folder, and give it permmission: 755.
—Copy Source Code—
<?php
error_reporting(0);
// Look for the "ip-ban.txt" and replace it with your file.
// The files contains IP addresses.
if ($handle = fopen("ip-ban.txt", "r+")) {
$ip = explode("
", fread($handle, filesize("ip-ban.txt")));
for ($i = 0; $i < count($ip); $i++) {
$ip[$i] = str_replace("*", "(.*)", $ip[$i]);
// Change the link here. But, don't remove "Location" from the header!
if (ereg($ip[$i], $_SERVER['REMOTE_ADDR'])) {
header("Location: http://google.com/");
}
} fclose($handle);
} ?>
—End Source Code—
Method Five – Blacklist & Redirection
—Copy Source Code—
<?php
$block_ips = array("000.000.000.000", "000.000.000.000");
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
foreach ($block_ips as $block_ip) {
if ($ip == $block_ip) {
die(header("Location: anypage.php"));
}
} ?>
—End Source Code—
Method Six – Blacklist portable v3
—Copy Source Code—
<?php
$deny = array(
// Add your offending IP adress here
"111.111.111",
"222.222.222",
"333.333.333"
);
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
header("location: http://www.google.com/");
exit();
} ?>
—End Source Code—
Method Seven – Blacklist Multifunctional
—Copy Source Code—
<?php
// Denied IP's.
//Comment these lines if you're using "$deny_ips = file".
$deny_ips = array(
'209.240.206.199',
'209.240.206.200',
'209.240.206.201',
'209.240.206.202',
);
// Create a txt file and use instead.
//Remove the comments bellow this line.
// $deny_ips = file('blocked_ips.txt');
// read user ip address:
$ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';
// search current IP in $deny_ips array
if (($i = array_search($ip, $deny_ips)) !== FALSE){
// user is blocked:
echo "Your IP address ('$ip') was blocked!";
exit;
}
// End of Blocking
?>
—End Source Code—
Method Eight – Blacklist Multifunctional v2
—Copy Source Code—
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ipArray = preg_replace("#\r\n?|\n#","",file('IP-Ban.txt')); // read the file into an array
foreach ($ipArray as $ipTest) {
if (substr_count($ip, $ipTest) != "0") {
die(header("Location: anypage.php"));
}
} ?>
—End Source Code—
4.] Finally, you’ll need to upload one of these files and use the codes to import this script.
5.] Done!
Copyrighted By Lair360







