您好,欢迎访问一九零五行业门户网

php实现根据IP地址获取其所在省市的方法_php实例

根据现有ip地址获取其地理位置(省份,城市等)的方法
function getiplookup($ip = ''){ if(empty($ip)){ return '请输入ip地址'; } $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip); if(empty($res)){ return false; } $jsonmatches = array(); preg_match('#\{.+?\}#', $res, $jsonmatches); if(!isset($jsonmatches[0])){ return false; } $json = json_decode($jsonmatches[0], true); if(isset($json['ret']) && $json['ret'] == 1){ $json['ip'] = $ip; unset($json['ret']); }else{ return false; } return $json; } $ipinfos = getiplookup('123.125.114.144'); //baidu.com ip地址 var_dump($ipinfos);
再发一个简化版的
function getipaddress(){ $ipcontent = file_get_contents(http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js); $jsondata = explode(=,$ipcontent); $jsonaddress = substr($jsondata[1], 0, -1); return $jsonaddress; } $ip_info=json_decode(getipaddress());
php根据ip地址实现城市切换或跳转
到这里,其实问题已经很简单了,用简单的js就通通搞定。c段如下:
//根据ip地址跳转指定页面js取得城市
var city=''; //根据ip地址所有城市跳转到指定页面
if(city.indexof(上海市)>=0){
         window.location.href=http://shanghai.demo.com/; }
将开头的a段代码和上面的c段代码分别放在b段代码的头和尾,然后我们在需要跳转的页面加入以下代码:
刷新页面,是不是达到预想的效果了呢?
以上所述就是本文的全部内容了,希望大家能够喜欢。
其它类似信息

推荐信息