利用JS脚本代码限制指定城市访问网站

有时候不想让某个城市的省或者区访问自己的网站?


方法一:

可以利用JS脚本代码进行限制:


<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" charset="GB2312" ></script>  
<script type=text/javascript>  
 if(remote_ip_info.city =='苏州'){  
  window.location.href="http://127.0.0.1";  
 }else{  
  ;  
 }  
</script>


代码的功能是利用新浪的IP识别功能,判断访问IP的所在城市省及区,如果判断结果相符合则跳转到别的链接地址,从而限制该地区对网站的访问。


需要说明的是

remote_ip_info.country----国家

remote_ip_info.province----省份

remote_ip_info.city ----市

remote_ip_info.district ----区


如果你不知道对应参数可以在浏览器打开地址测试:

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=xxx.xxx.xxx.xxx  (xxx.xxx.xxx.xxx为测试的IP地址)


方法二:


<script type="text/javascript" src="//apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$.getJSON("http://ip-api.com/json/?callback=?",function(d){
if(d.regionName=='Jiangsu')
window.location.href="http://www.baidu.com";
});
</script>


具体参数请访问http://ip-api.com/



其他IP地址查询接口


http://ip-api.com/json/

http://ip.taobao.com/service/getIpInfo.php?ip=

http://ip.ws.126.net/ipquery

http://pv.sohu.com/cityjson

http://www.geoplugin.net/json.gp

http://gd.geobytes.com/GetCityDetails

https://json.geoiplookup.io/api

https://ipapi.co/json/

https://api.ipdata.co


Bài viết này đến từ http://www.chieng.cn, được sao chép, vui lòng ghi rõ!
打赏 支付宝打赏 微信打赏

评论

Top