header("Content-type: text/html; charset=utf-8"); function getCity($ip = '')//獲取地區 { if($ip == ''){ $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";//新浪借口獲取訪問者地區 $ip=json_decode(file_get_contents($url),true); $data = $ip; }else{ $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;//淘寶借口需要填寫ip $ip=json_decode(file_get_contents($url)); if((string)$ip->code=='1'){ return false; } $data = (array)$ip->data; } return $data; } print_r($city); //獲取訪問者ip地址 if ($_SERVER['REMOTE_ADDR']) {//判斷SERVER里面有沒有ip,因為用戶訪問的時候會自動給你網這里面存入一個ip $cip = $_SERVER['REMOTE_ADDR']; } elseif (getenv("REMOTE_ADDR")) {//如果沒有去系統變量里面取一次 getenv()取系統變量的方法名字 $cip = getenv("REMOTE_ADDR"); } elseif (getenv("HTTP_CLIENT_IP")) {//如果還沒有在去系統變量里取下客戶端的ip $cip = getenv("HTTP_CLIENT_IP"); } else { $cip = "unknown"; } echo $cip;