技术文章

PHP 限制国内IP访问 代码实例

日期:2018-09-04 阅读:27055

https://blog.csdn.net/forest_fire/article/details/50944721

主要是利用淘宝的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