A common code function to get IP GetIP () {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 ("Remot
E_addr "); else if (isset ($_server[' remote_addr ']) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_
ADDR '], "unknown")) {$ip = $_server[' remote_addr '];
else {$ip = "unknown";
return $IP;
function getLocation ($ip = ') {empty ($IP) && $ip = GetIP ();
if ($ip = = "127.0.0.1") return "native address";
$api = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip= $ip"; $json = file_get_contents ($API); Call Sina IP Address library $arr = JSON_decode ($json, TRUE); Parse json $country = $arr [' Country ']; Acquisition of national $province = $arr [' Province ']; Get the province $city = $arr [' City ']; Get City if ((string) $country = = "China") {if ((string) ($province)!= (string) $city) {$_location = $prov
Ince. $city;
}else{$_location = $country. $city;
}}else{$_location = $country;
return $_location; Echo GetLocation (); or Echo getLocation ("115.24.28.238");