<del id="7msxa"><table id="7msxa"><strong id="7msxa"></strong></table></del><legend id="7msxa"></legend>

<tbody id="7msxa"></tbody>
  • <ol id="7msxa"></ol><th id="7msxa"><track id="7msxa"><rt id="7msxa"></rt></track></th>
    1. <dd id="7msxa"></dd>
    2. 首頁 > 編程 > PHP > 正文

      PHP根據IP地址獲取所在城市

      2020-03-22 20:29:59
      字體:
      來源:轉載
      供稿:網友
    3. 有這樣的需求,需要根據用戶的IP地址,定位用戶所在的城市。

      本文記錄性文章,無邏輯性。有這樣需求的朋友,可以直接拷貝使用。直接上代碼,不需贅述。

      <?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_FORWARDED_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("REMOTE_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地址var_dump($ipInfos);

      結果截圖:

      使用了新浪的開放API,執行速度還是非常贊的。

      出處!http://blog.csdn.net/liruxing1715/article/details/28238483

      PHP編程

      鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

    4. 發表評論 共有條評論
      用戶名: 密碼:
      驗證碼: 匿名發表
      japan日本人妻熟老太
      <del id="7msxa"><table id="7msxa"><strong id="7msxa"></strong></table></del><legend id="7msxa"></legend>

      <tbody id="7msxa"></tbody>
    5. <ol id="7msxa"></ol><th id="7msxa"><track id="7msxa"><rt id="7msxa"></rt></track></th>
      1. <dd id="7msxa"></dd>