根据IP定位用户所在城市信息

1.调用新浪IP地址库

新浪提供了开放的IP地址库数据供开发者调用,调用地址:

http://int.dpool.sina.com.cn/iplookup/iplookup.php,即可返回当前ip所在的省市信息。

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript" src="js/jquery.cityselect.js"></script> 
<script type="text/javascript" src="http://int.dpool.sina.com.cn/iplookup/iplookup.php? 
format=js"></script> 

我们先载入jquery库和cityselect城市下拉插件,然后调用新浪的IP地址库,并以js的形式返回,当然如果你想查询某一指定IP所在的城市信息可以使用接口地址如:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=123.123.123.123,将参数ip值换成指定的IP地址即可。

本地js调用:

var myprovince = remote_ip_info['province']; 
var mycity = remote_ip_info['city'] 
var mydistrict = remote_ip_info['district']; 
$(function(){ 
    $("#city_1").citySelect({ 
        prov:myprovince,  
        city:mycity 
    }); 
});

HTML代码为:

<h3>调用新浪IP库接口</h3> 
<p>您所在的城市是:<script>document.write(myprovince+' '+mycity);</script></p> 
<div id="city_1"> 
    <select class="prov"></select>  
    <select class="city"></select> 
</div> 

2.调用淘宝IP地址库

淘宝也提供了比较权威的IP地址库,调用地址:http://ip.taobao.com/service/getIpInfo.php?ip=123.123.123.123,返回对应IP的省市相关信息。

调用方法:

$(function(){ 
    $.getJSON("getTaoIP.php",function(json){ 
        var myprovince2 = json.data.region;         
        var mycity2 = json.data.city; 
        $("#city_2").html("您所在的城市是:"+myprovince2+mycity2); 
    }); 
}); 

getTaoIP.php用来获取淘宝的对应IP的省市信息,返回的是json格式的数据。

$ip = get_client_ip(); //获取当前用户的ip 
$url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; 
$data = file_get_contents($url); //调用淘宝接口获取信息 
echo $data;

get_client_ip()用来获取本地用户的IP地址。

//获取用户真实IP 
function get_client_ip() { 
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) 
        $ip = getenv("HTTP_CLIENT_IP"); 
    else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),  
"unknown")) 
        $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) 
        $ip = getenv("REMOTE_ADDR"); 
    else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']  
&& strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 
        $ip = $_SERVER['REMOTE_ADDR']; 
    else 
        $ip = "unknown"; 
    return ($ip); 
} 

案例代码分享

数据表

DROP TABLE IF EXISTS `think_ipdb`;
CREATE TABLE `think_ipdb` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(30) DEFAULT NULL,
  `country` varchar(50) DEFAULT NULL,
  `province` varchar(30) DEFAULT NULL,
  `city` varchar(30) DEFAULT NULL,
  `district` varchar(30) DEFAULT NULL,
  `isp` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ip` (`ip`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=24780 DEFAULT CHARSET=utf8 COMMENT='本地 IP地址库';

调用实例

$think_ipdb = M('think_ipdb',null,C('UC_DB_DSN')); // 实例化User对象
$city = $this->_getLocalDbIp($think_ipdb,$value['ip']);

类方法

/**
 * 获取本地ip库信息
 * @param type $db
 * @param type $ip
 */
private function _getLocalDbIp($db,$ip) {
    if(empty($ip)){
        return null;
    }
    $tmp = $db->where(array('ip'=>$ip))->find();
    if(empty($tmp)){
        $address = getAddressFromIp($ip);
        if(!empty($address)){
            $address['ip'] = $ip;
            $db->add($address);
            return $address['city'];
        }else{
            return null;
        }
    }else{
        $city = $tmp['city'];
    }
}

通用函数

//根据ip地址获取地址信息
function getAddressFromIp($ip){
    $urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
    $urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
    $json = file_get_contents($urlTaobao);
    $jsonDecode = json_decode($json);
    if($jsonDecode->code==0){//如果取不到就去取新浪的
        $data['country'] = $jsonDecode->data->country;
        $data['province'] = $jsonDecode->data->region;
        $data['city'] = $jsonDecode->data->city;
        $data['isp'] = $jsonDecode->data->isp;
        return $data;
    }else{
        $json = file_get_contents($urlSina);
        $jsonDecode = json_decode($json);
        $data['country'] = $jsonDecode->country;
        $data['province'] = $jsonDecode->province;
        $data['city'] = $jsonDecode->city;
        $data['isp'] = $jsonDecode->isp;
        $data['district'] = $jsonDecode->district;
        return $data;
    }
}

//根据ip地质获取城市名
function getCityFromIp($ip){
    $data = getAddressFromIp($ip);
    return $data['city'];
}