PHP获取用户真实 IP , 淘宝IP接口获得ip地理位置(转)
1 <?php 2 /** 3 * 获取用户真实 IP 4 */ 5 function getIP() 6 { 7 static $realip; 8 if (isset($_SERVER)){ 9 if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ 10 $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 11 } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { 12 $realip = $_SERVER["HTTP_CLIENT_IP"]; 13 } else { 14 $realip = $_SERVER["REMOTE_ADDR"]; 15 } 16 } else { 17 if (getenv("HTTP_X_FORWARDED_FOR")){ 18 $realip = getenv("HTTP_X_FORWARDED_FOR"); 19 } else if (getenv("HTTP_CLIENT_IP")) { 20 $realip = getenv("HTTP_CLIENT_IP"); 21 } else { 22 $realip = getenv("REMOTE_ADDR"); 23 } 24 } 25 26 27 return $realip; 28 }
1 /** 2 * 获取 IP 地理位置 3 * 淘宝IP接口 4 * @Return: array 5 */ 6 function getCity($ip) 7 { 8 $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; 9 $ip=json_decode(file_get_contents($url)); 10 if((string)$ip->code=='1'){ 11 return false; 12 } 13 $data = (array)$ip->data; 14 return $data; 15 }