獲取IP地址的幾種方法


根據ip獲取地址的幾種方法
1.調用新浪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地址即可。
本地使用:
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']; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM