php怎么禁止国内ip访问网站
- 建站笔记
- 2022-05-10
- 759
php禁止国内ip访问网站的方法:1、通过“$_SERVER['REMOTE_ADDR']”获取ip地址;2、通过“if((!empty($banned['data']['country_id']){...}”判断并禁止国内ip访问网站。
本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑
php怎么禁止国内ip访问网站?
用php代码限制国内IP访问我们网站
原理:
利用淘宝的IP接口来判断IP,是否是国内的ip,是国内(CN)的就不允许访问。
代码如下:
$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; }
以上就是php怎么禁止国内ip访问网站的详细内容,更多请关注白琉璃源码网其它相关文章!
本文由白琉璃于2022-05-10发表在白琉璃源码网,如有侵权或疑问,请联系我们,谢谢。
本文链接:https://www.bailiuli.com/t/2993.html
发表评论