Skip to content

Instantly share code, notes, and snippets.

@anytizer
Created July 28, 2014 05:19
Show Gist options
  • Save anytizer/193b396b86c3ce0d8088 to your computer and use it in GitHub Desktop.
Save anytizer/193b396b86c3ce0d8088 to your computer and use it in GitHub Desktop.
Blocking visitors based on their IP Addresses
How to Block IP Addresses with PHP
http://perishablepress.com/how-to-block-ip-addresses-with-php/
<?php
$deny = array("111.111.111", "222.222.222", "333.333.333");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
header("location: http://www.google.com/");
exit();
} ?>
http://kb.site5.com/security/how-to-automatically-block-someone-using-a-php-script/
How to Automatically Block Someone using a PHP Script
http://www.blackdog.ie/blog/blocking-ip-ranges-with-php/
Blocking IP ranges with PHP
Block multiple IP addresses with PHP
http://grohsfabian.com/tag/how-to-block-specific-ip-addresses-php/
function blockIps($blocked_ip_list){
$ip = $_SERVER['REMOTE_ADDR'];
if(in_array($ip, $blocked_ip_list)){
die("Your IP(" . $ip . ") has been blocked !");
}
}
$blocked_ip_list = array("127.0.0.1","127.0.0.2");
blockIps($blocked_ip_list);
?>
http://stackoverflow.com/questions/2869893/block-specific-ip-block-from-my-website-in-php
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));
// only allow 10.0.0.0 - 10.255.255.255
if (!($ip >= 167772160 && $ip <= 184549375)) {
die('Forbidden.');
}
Apache .htaccess Guide & Tutorial >>
Deny visitors by IP address
// The blacklisted ips.
$denied_ips = array(
'1.2.3.4',
'2.3.*',
);
// The function to get the visitor's IP.
function getUserIP(){
//check ip from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
//to check ip is pass from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//The user
$visitorIp = getUserIP();
// Now let's search if this IP is blacklisted
$status = array_search($visitorIp, $denied_ips);
// Let's check if $status has a true OR false value.
if($status !== false){
echo '<div class="error">Your IP has been banned! Stop spamming us!</div>';
// header("Location: http://zombo.com");
// exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment