PHP 限制国内IP访问 代码实例
2013 年 9 月 5 日 – 00:52 | 1,688 views | 收藏主要是利用淘宝的IP接口来判断IP 是否是国内的ip 是国内的就不允许访问
$ip = $_SERVER['REMOTE_ADDR'];
$content = file_get_contents(‘http://ip.taobao.com/service/getIpInfo.php?ip=’.$ip);
$banned = json_decode(trim($content), true);
$lan = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if((!empty($banned['data']['country_id']) && $banned['data']['country_id'] == ‘CN’) || strstr($lan, ‘zh’))
{
header(“HTTP/1.0 404 Not Found”);
echo ‘HTTP/1.0 404 Not Found’;
exit;
}
http://ip.taobao.com/service/getIpInfo.php?ip=112.124.103.61
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u4e1c","area_id":"300000","region":"\u6d59\u6c5f\u7701","region_id":"330000","city":"\u676d\u5dde\u5e02","city_id":"330100","county":"","county_id":"-1","isp":"\u963f\u91cc\u4e91","isp_id":"1000323","ip":"112.124.103.61"}}