Http://www.smohan.net/blog/14.html
Before the station using Taobao interface to achieve IP and geographical information conversion, seemingly recently Taobao IP server error, resulting in file_get_contents can not get content and JSON return, so, for a more stable Sina IP address library to achieve some of the functions of this site. Sina provides open IP address library data for developers to call, address: http://int.dpool.sina.com.cn/iplookup/iplookup.php, you can return the current IP in the provinces and cities of information. Specific Demo demo Please refer to the site message
First: Let's use a frequently seen function getip () to achieve IP acquisition:
function 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 ("REMOTE_ADDR"); else if (isset ($_server[' remote_addr ']) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], "unknown")) {$ip = $_server[' remote_addr '];} else {$ip = "Unknown";} return $IP; }
Close Code
The second step: the IP address will be obtained by passing a Sina IP interface, using a commonly used function file_get_contents to obtain its JSON format return value, so that we can achieve the IP address and the corresponding provincial city conversion;
function getLocation ($ip = ') {empty ($IP) && $ip = GetIP (); if ($ip = = "127.0.0.1") return "native address"