手机
当前位置:查字典教程网 >编程开发 >php教程 >php实现根据IP地址获取其所在省市的方法
php实现根据IP地址获取其所在省市的方法
摘要:根据现有IP地址获取其地理位置(省份,城市等)的方法functionGetIpLookup($ip=''){if(empty($ip)){r...

根据现有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"); $jsonData = explode("=",$ipContent); $jsonAddress = substr($jsonData[1], 0, -1); return $jsonAddress; } $ip_info=json_decode(getIpAddress());

PHP根据IP地址实现城市切换或跳转

到这里,其实问题已经很简单了,用简单的js就通通搞定。C段如下:

//根据IP地址跳转指定页面js取得城市

var city='<?echo ipCity($xp_UserIp);?>'; //根据IP地址所有城市跳转到指定页面

if(city.indexOf("上海市")>=0){

window.location.href="http://shanghai.demo.com/"; }

将开头的A段代码和上面的C段代码分别放在B段代码的头和尾,然后我们在需要跳转的页面加入以下代码:

<script src="http://www.jb51.netipcity/ipaddress.php" type="text/javascript" language="javascript"></script>

刷新页面,是不是达到预想的效果了呢?

以上所述就是本文的全部内容了,希望大家能够喜欢。

【php实现根据IP地址获取其所在省市的方法】相关文章:

php获取twitter最新消息的方法

php实现读取内存顺序号

php生成圆角图片的方法

php转换颜色为其反色的方法

php实现读取和写入tab分割的文件

php删除指定目录的方法

php实现TCP端口检测的方法

PHP获取远程图片并保存到本地的方法

PHP准确取得服务器IP地址的方法

php获取网页上所有链接的方法

精品推荐
分类导航