用php代码限制国外IP只允许国内地址访问网站

实现原理,利用ip地址查询识别国家地区+浏览器语言判断。 以爱奇艺ip查询接口为例,禁止国内ip访问。

ip地址中国或者浏览器语言为zh时:

$ipaddress = $_SERVER['REMOTE_ADDR'] == '::1' ? '127.0.0.1' : $_SERVER['REMOTE_ADDR']
 $lang = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
 $login_addr_arra = json_decode(file_get_contents('http://ip.geo.iqiyi.com/cityjson?fo
 $country = $login_addr_arra->data->country;
 if(!empty($country) && $country == '中国') || strstr($lang, 'zh')
 {
 header("ip地址为中国");
 echo 'ip地址为中国';
 exit;
}

wordpress程序只需要添加到根目录下的index.php文件第二行即可。 

禁止国外访问,ip地址和浏览器语言两个条件同时满足:

$ipaddress = $_SERVER['REMOTE_ADDR'] == '::1' ? '127.0.0.1' : $_SERVER['REMOTE_ADDR']
$lang = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$login_addr_arra = json_decode(file_get_contents('http://ip.geo.iqiyi.com/cityjson?fo
$country = $login_addr_arra->data->country;
if($country !== '中国') && (!strstr($lang, 'zh'))
{
header("ip地址为国外");
echo 'ip地址为国外';
exit;
}else{
 echo '中国';
 }


© 版权声明
THE END