基于我们有些运维需求,需要屏蔽指定国家和地区的用户IP访问。这里,我们可以用特定的PHP脚本撰写检测指定IP的地区IP,然后通过引用到WordPress嵌入到页面中。这里,麦子整理几个网上采用的方法。后续如果有需要的时候可以一并参考选择用到我们网站中。
1、屏蔽指定国家
<?php /** * * test.php(屏蔽国家IP) * */ $verification = '美国';//需要屏蔽国家的IP function get_client_ip() { $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_X_REAL_FORWARDED_FOR']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_REAL_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } return $ip; } $ip = get_client_ip();//获取访客IP $antecedents = $_SERVER['HTTP_REFERER'];//访客来路地址 $result = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//IP数据库来自淘宝。 $address = json_decode($result,true); //判断访客是否属于美国,是否来自百度,是否来自谷歌 if($address['data']['country'] == $verification && strpos($antecedents, 'baidu') === false && strpos($antecedents, 'google') === false){ sleep(10);//设置一个10秒等待。 header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 3600000'); exit; } ?>
这里,我们将代码丢到test.php页面中然后引用到index.php或者是header.php中。
2、屏蔽指定省份
<?php /** * * test.php(屏蔽地方IP) * */ $verification = '江西省';//需要屏蔽省份的IP function get_client_ip() { $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_X_REAL_FORWARDED_FOR']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_REAL_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } return $ip; } $ip = get_client_ip();//获取访客IP $antecedents = $_SERVER['HTTP_REFERER'];//访客来路地址 $result = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//IP数据库来自淘宝。 $address = json_decode($result,true); //判断访客是否属于江西省,是否来自百度,是否来自谷歌 if($address['data']['region'] == $verification && strpos($antecedents, 'baidu') === false && strpos($antecedents, 'google') === false){ sleep(99999999);//设置一个999999秒的等待。 Header("HTTP/1.1 204 No Content"); exit; } ?>
3、屏蔽指定访客不跳过搜索引擎蜘蛛
<?php function get_client_ip() { if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR']; else $ip = "unknown"; return ($ip); } header("Content-type: text/html; charset=utf-8"); $verification = '山东省';//修改省份 $ip = get_client_ip(); //获取当前用户的ip $antecedents = $_SERVER['HTTP_REFERER'];//访客来路地址 $result = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//IP数据库来自淘宝。 $address = json_decode($result,true); if($address['data']['region'] == $verification && strpos($antecedents, 'baidu') === false && strpos($antecedents, 'google') === false){ sleep(9999999999999999); ob_start(); header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 3600'); echo '网站备案中....'; exit; } ?>
根据需要修改对应省份。这个是可以屏蔽对应指定访客地区的,但是不屏蔽蜘蛛。
参考文章:
1、https://www.cnblogs.com/igood/p/4802114.html
2、https://wzfou.com/ip-ku/
评论