How PHP gets the city based on the IP address

Source: Internet
Author: User
This article mainly to share with you how PHP based on IP address to obtain the city, this article mainly in the form of code, I hope to help everyone.

<?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 ();    } $res = @file_get_contents (' http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip= '. $ip);    if (empty ($res)) {return false;}    $jsonMatches = Array ();    Preg_match (' #\{.+?\}# ', $res, $jsonMatches);    if (!isset ($jsonMatches [0])) {return false;} $json = Json_decode ($jsonMatches [0], true);        if (Isset ($json [' ret ']) && $json [' ret '] = = 1) {$json [' IP '] = $IP;    unset ($json [' ret ']);    }else{return false; } return $json;} $ipInfos = Getiplookup (' 123.125.114.144 '); Baidu.com IP 鍦 Board from Var_dump ($ipInfos);? >
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.