欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)根據(jù)IP地址獲取其所在省市的方法

 更新時(shí)間:2015年04月30日 10:12:59   投稿:hebedich  
今天,就討論一下在PHP中如何獲取用戶IP地址、PHP根據(jù)IP判斷用戶所在城市以及PHP根據(jù)IP實(shí)現(xiàn)城市切換或跳轉(zhuǎn)的問題。

根據(jù)現(xiàn)有IP地址獲取其地理位置(省份,城市等)的方法

function GetIpLookup($ip = ''){  
  if(empty($ip)){  
    return '請(qǐng)輸入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); 

再發(fā)一個(gè)簡化版的

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根據(jù)IP地址實(shí)現(xiàn)城市切換或跳轉(zhuǎn)

到這里,其實(shí)問題已經(jīng)很簡單了,用簡單的js就通通搞定。C段如下:

//根據(jù)IP地址跳轉(zhuǎn)指定頁面js取得城市

var city='<?echo ipCity($xp_UserIp);?>'; //根據(jù)IP地址所有城市跳轉(zhuǎn)到指定頁面

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

         window.location.href=" 將開頭的A段代碼和上面的C段代碼分別放在B段代碼的頭和尾,然后我們在需要跳轉(zhuǎn)的頁面加入以下代碼:

<script src="/ipcity/ipaddress.php" type="text/javascript" language="javascript"></script>
刷新頁面,是不是達(dá)到預(yù)想的效果了呢?

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論