正 文:
通过IP获取用户地理位置省份城市的接口使用,判断用户ip获取地理位置后,禁止指定的省份或城市用户访问网站或跳转到404页面:
新浪ip接口:
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js使用示例:
<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>
<script>
var province=remote_ip_info.province;
var city=remote_ip_info.city;
if(province=="" || province.indexOf("江苏")>-1 || province.indexOf("广东")>-1){
if(city.indexOf("无锡")==-1) window.location.href="/404.htm";
}
</script>
新浪的的多地域接口:
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42直接返回:
var remote_ip_info = {"ret":1,"start":"218.192.0.0","end":"218.192.7.255","country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"\u6559\u80b2\u7f51","type":"\u5b66\u6821","desc":"\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662\u6559\u80b2\u7f51"};
腾讯的ip接口(已被封):
http://fw.qq.com/ipaddress 返回值
var IPData = new Array("58.218.198.205","","江苏省","徐州市");
搜狐的ip接口:搜狐IP地址查询接口(默认GBK):
http://pv.sohu.com/cityjson搜狐IP地址查询接口(可设置编码):
http://pv.sohu.com/cityjson?ie=utf-8返回:
var returnCitySN = {"cip": "121.235.101.138", "cid": "320200", "cname": "江苏省无锡市"};
使用PHP2 Template:
http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=61.51.71.183返回:
返回的数组信息如下,是英文版本的,需适当处理下
Array
(
[known] => true //该IP地址是否可知,即是否有收录;
[locationcode] => CNGDGUAN //地址位置的代码,包含了国家、省份和城市;
[fips104] => CH //美国联邦信息处理标准的国家代码;
[iso2] => CN //iso2标准的国家代码;
[iso3] => CHN //iso标准的国家代码;
[ison] => 156 //用途未明
[internet] => CN //也是国家代码
[countryid] => 49 //国家ID;
[country] => China //国家名称;
[regionid] => 1361 //地区的id,即省份;
[region] => Guangdong //地区名称,即省份名称;
[regioncode] => GD //地区的代码或者缩写;
[adm1code] => CH30 //不清楚其含义;
[cityid] => 3539 //城市的ID;
[city] => Guangzhou //城市的名称;
[latitude] => 23.1170 //纬度;
[longitude] => 113.2500 //经度;
[timezone] => +08:00 //时区;
[certainty] => 78 //不清楚其含义;
)
其实,我们还可以利用百度来获取ip地域信息。