首頁 >後端開發 >php教程 >PHP 根據IP位址取得所在城市 php取得用戶ip php ip php取得伺服器i

PHP 根據IP位址取得所在城市 php取得用戶ip php ip php取得伺服器i

WBOY
WBOY原創
2016-07-29 08:50:431122瀏覽

有這樣的需求,需要依照使用者的IP位址,定位使用者所在的城市。
本文記錄性文章,無邏輯性。有這樣需求的朋友,可以直接拷貝使用。直接上程式碼,不需贅述。

<code><span><?php</span>
header(<span>'Content-Type:text/html;Charset=utf-8'</span>);
<span><span>function</span><span>GetIp</span><span>()</span>{</span><span>$realip</span> = <span>''</span>;
    <span>$unknown</span> = <span>'unknown'</span>;
    <span>if</span> (<span>isset</span>(<span>$_SERVER</span>)){
        <span>if</span>(<span>isset</span>(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>]) && !<span>empty</span>(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>]) && strcasecmp(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>], <span>$unknown</span>)){
            <span>$arr</span> = explode(<span>','</span>, <span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>]);
            <span>foreach</span>(<span>$arr</span><span>as</span><span>$ip</span>){
                <span>$ip</span> = trim(<span>$ip</span>);
                <span>if</span> (<span>$ip</span> != <span>'unknown'</span>){
                    <span>$realip</span> = <span>$ip</span>;
                    <span>break</span>;
                }
            }
        }<span>else</span><span>if</span>(<span>isset</span>(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>]) && !<span>empty</span>(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>]) && strcasecmp(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>], <span>$unknown</span>)){
            <span>$realip</span> = <span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>];
        }<span>else</span><span>if</span>(<span>isset</span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]) && !<span>empty</span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]) && strcasecmp(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>], <span>$unknown</span>)){
            <span>$realip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>];
        }<span>else</span>{
            <span>$realip</span> = <span>$unknown</span>;
        }
    }<span>else</span>{
        <span>if</span>(getenv(<span>'HTTP_X_FORWARDED_FOR'</span>) && strcasecmp(getenv(<span>'HTTP_X_FORWARDED_FOR'</span>), <span>$unknown</span>)){
            <span>$realip</span> = getenv(<span>"HTTP_X_FORWARDED_FOR"</span>);
        }<span>else</span><span>if</span>(getenv(<span>'HTTP_CLIENT_IP'</span>) && strcasecmp(getenv(<span>'HTTP_CLIENT_IP'</span>), <span>$unknown</span>)){
            <span>$realip</span> = getenv(<span>"HTTP_CLIENT_IP"</span>);
        }<span>else</span><span>if</span>(getenv(<span>'REMOTE_ADDR'</span>) && strcasecmp(getenv(<span>'REMOTE_ADDR'</span>), <span>$unknown</span>)){
            <span>$realip</span> = getenv(<span>"REMOTE_ADDR"</span>);
        }<span>else</span>{
            <span>$realip</span> = <span>$unknown</span>;
        }
    }
    <span>$realip</span> = preg_match(<span>"/[\d\.]{7,15}/"</span>, <span>$realip</span>, <span>$matches</span>) ? <span>$matches</span>[<span>0</span>] : <span>$unknown</span>;
    <span>return</span><span>$realip</span>;
}

<span><span>function</span><span>GetIpLookup</span><span>(<span>$ip</span> = <span>''</span>)</span>{</span><span>if</span>(<span>empty</span>(<span>$ip</span>)){
        <span>$ip</span> = GetIp();
    }
    <span>$res</span> = @file_get_contents(<span>'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='</span> . <span>$ip</span>);
    <span>if</span>(<span>empty</span>(<span>$res</span>)){ <span>return</span><span>false</span>; }
    <span>$jsonMatches</span> = <span>array</span>();
    preg_match(<span>'#\{.+?\}#'</span>, <span>$res</span>, <span>$jsonMatches</span>);
    <span>if</span>(!<span>isset</span>(<span>$jsonMatches</span>[<span>0</span>])){ <span>return</span><span>false</span>; }
    <span>$json</span> = json_decode(<span>$jsonMatches</span>[<span>0</span>], <span>true</span>);
    <span>if</span>(<span>isset</span>(<span>$json</span>[<span>'ret'</span>]) && <span>$json</span>[<span>'ret'</span>] == <span>1</span>){
        <span>$json</span>[<span>'ip'</span>] = <span>$ip</span>;
        <span>unset</span>(<span>$json</span>[<span>'ret'</span>]);
    }<span>else</span>{
        <span>return</span><span>false</span>;
    }
    <span>return</span><span>$json</span>;
}


<span>$ipInfos</span> = GetIpLookup(<span>'123.125.114.144'</span>); <span>//baidu.com IP地址</span>
var_dump(<span>$ipInfos</span>);</code>

使用了新浪的開放API,執行速度還是非常讚的。 本文來自 CSDN,轉載請標註出處! http://blog.csdn.net/liruxing1715/article/details/28238483

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了PHP 根據IP位址取得所在城市,包括了ip位址,php方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn