In the usage of the predefined variable $_server, we can find that the function of the parameter ' REMOTE_ADDR ' is to browse the IP address of the user of the current page.
According to this we can obtain the client's IP, according to the acquired IP we can further obtain other information such as the location of each other.
This section is mainly about how to get the IP address of the client and locate its location. Let's look at the code implementation first:
<?phpheader (' content-type:text/html; Charset=utf-8 '); function GetIP () {$realip = '; $unknown = ' unknown '; if (Isset ($_server)) {if (Isset ($_server[' http_x_forwarded_for ')) &&!empty ($_server[' http_x_forwarded_for ') ]) && strcasecmp ($_server[' http_x_forwarded_for '), $unknown)) {$arr = explode (', ', $_server[' http_x_for Warded_for ']); foreach ($arr as $ip) {$ip = Trim ($IP); if ($ip! = ' Unknown ') {$realip = $ip; Break }}}else if (Isset ($_server[' http_client_ip ') &&!empty ($_server[' http_client_ip ']) && STRCASECMP ($_server[' http_client_ip '), $unknown)) {$realip = $_server[' http_client_ip ']; }else if (isset ($_server[' remote_addr ") &&!empty ($_server[' remote_addr ']) && strcasecmp ($_server[') Remote_addr '], $unknown)) {$realip = $_server[' remote_addr '); }else{$reaLip = $unknown; }}else{if (getenv (' http_x_forwarded_for ') && strcasecmp (getenv (' http_x_forwarded_for '), $unknown)) { $realip = getenv ("Http_x_forwarded_for"); }else if (getenv (' http_client_ip ') && strcasecmp (getenv (' http_client_ip '), $unknown)) {$realip = getenv ( "Http_client_ip"); }else if (getenv (' remote_addr ') && strcasecmp (getenv (' remote_addr '), $unknown)) {$realip = getenv ("Remot E_addr "); }else{$realip = $unknown; }} $realip = Preg_match ("/[\d\.") {7,15}/", $realip, $matches)? $matches [0]: $unknown; return $realip;} function Getiplookup ($ip = ") {if (empty ($ip)) {$ip = GetIP (); } $address = @file_get_contents (' http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip= '. $ip); if (empty ($address)) {return false;} $jsonMatches = Array (); Preg_match (' #\{.+?\}# ', $address, $jsonMatches); if (!isset ($jsonMatches [0])) {return false; } $address _json = Json_decode ($jsonMatches [0], true); if (isset ($address _json[' ret ') && $address _json[' ret '] = = 1) {$address _json[' ip '] = $IP; unset ($address _json[' ret '); }else{return false; } return $address _json;} $ipInfos = Getiplookup (); Var_dump ($ipInfos); echo "<br>", echo "your address:". $ipInfos ["Country"]. $ipInfos ["Province" ]. $ipInfos ["City"]. "<br>";
Deploy the code to the SAE and run the following results:
From the results of the run, you can see that the content contained in the $address _json is:
Array (Ten) {["Start"]=> Int ( -1) ["End"]=> Int ( -1) ["Country"]=> string (6) "China" ["Province"]=> string (6) "Sichuan" [ "City"]=> string (6) "Chengdu" ["District"]=> string (0) "" ["ISP"]=> string (0) "" ["Type"]=> string (0) "" ["desc"]=& Gt String (0) "" ["IP"]=> string (13) "121.48.152.31"}
We just need to extract the information from the display.
This Access user location uses the API provided by Sina, and can be implemented according to the actual situation using other APIs. According to the IP can also get the location of weather, traffic and other information, interested people can be discussed together.
Get its location based on user IP