tp5 通過IP獲取對應ip的城市
阿新 • • 發佈:2019-02-09
1 public function test() 2 { 3 $request = Request::instance(); 4 $ip = $request->ip(); 5 $url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip; 6 $result = file_get_contents($url); 7 $result = json_decode($result,true); 8 dump($result);
9 }
// 定義一個函式getIP() 客戶端IP,
function getIP(){
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else $ip = "Unknow";
if(preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip))
return $ip;
else
return '';
}
// 伺服器端IP
function serverIP(){
return gethostbyname($_SERVER["SERVER_NAME"]);
}