PHP 限制国内IP访问

主要是利用淘宝的IP接口来判断IP 是否是国内的ip 是国内的就不允许访问

 $ip = $_SERVER['REMOTE_ADDR'];
// echo $ip;
 //$ip = '112.124.103.61'; //国内
  //$ip = '74.125.68.93'; //国外
$ipcontent = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
//echo $ipcontent;
 
$banned = json_decode(trim($ipcontent), true);
//$lan = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
//if((!empty($banned['data']['country_id']) && $banned['data']['country_id'] == 'CN') || strstr($lan, 'zh'))
if(!empty($banned['data']['country_id']) && $banned['data']['country_id'] == 'CN')
{
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

-----------------

如果ip接口失效,通过判断浏览器:

if( strpos(strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]),'zh-cn') !== false ){

header('HTTP/1.0 404 Not Found');

echo 'HTTP/1.0 404 Not Found';

exit;

}

你可能感兴趣的