There is a need to locate the user's city based on the user's IP address.
This article is a documented article, without logic. Friends with such needs can be copied directly to use. Directly on the code, do not need to repeat.
Header' content-type:text/html; Charset=utf-8 '); functiongetip(){$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_forwarded_for ']);foreach($arr as$ip){$ip= Trim ($ip);if($ip!=' Unknown '){$realip=$ip; Break; } } }Elseif(isset($_server[' Http_client_ip ']) &&!Empty($_server[' Http_client_ip ']) && strcasecmp ($_server[' Http_client_ip '],$unknown)){$realip=$_server[' Http_client_ip ']; }Elseif(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"); }Elseif(Getenv (' Http_client_ip ') && strcasecmp (getenv (' Http_client_ip '),$unknown)){$realip= Getenv ("Http_client_ip"); }Elseif(Getenv (' REMOTE_ADDR ') && strcasecmp (getenv (' REMOTE_ADDR '),$unknown)){$realip= Getenv ("REMOTE_ADDR"); }Else{$realip=$unknown; } }$realip= Preg_match ("/[\d\." {7,15}/",$realip,$matches) ?$matches[0] :$unknown;return$realip;} functiongetiplookup($ip = "){if(Empty($ip)){$ip= GetIP (); }$res= @file_get_contents (' http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip= '.$ip);if(Empty($res)){returnfalse; }$jsonMatches=Array(); Preg_match ('#\{.+?\}#',$res,$jsonMatches);if(!isset($jsonMatches[0])){returnfalse; }$json= Json_decode ($jsonMatches[0],true);if(isset($json[' ret ']) &&$json[' ret '] ==1){$json[' IP '] =$ip;unset($json[' ret ']); }Else{returnfalse; }return$json;}$ipInfos= Getiplookup (' 123.125.114.144 ');//baidu.com IP AddressVar_dump ($ipInfos);
Using Sina's Open API, the execution speed is still very great. This article is from CSDN, reprint please mark the source! http://blog.csdn.net/liruxing1715/article/details/28238483
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the PHP based on the IP address to obtain the city, including the IP address, PHP content, I hope the PHP tutorial interested in a friend helpful.