Home  >  Article  >  Backend Development  >  PHP gets the city based on the IP address php gets the user ip php ip php gets the server i

PHP gets the city based on the IP address php gets the user ip php ip php gets the server i

WBOY
WBOYOriginal
2016-07-29 08:50:431092browse

There is such a demand that the user's city needs to be located based on the user's IP address.
This article is a documentary article and has no logic. Friends who have such needs can copy and use it directly. Go directly to the code, no need to go into details.

<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>

Using Sina’s open API, the execution speed is still very good. This article comes from CSDN, please indicate the source when reprinting! 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); }); });

The above introduces PHP to obtain the city based on the IP address, including the IP address and php content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn