Home  >  Article  >  Backend Development  >  Determine the area by IP address. Determine the correctness of the IP address. How to judge the correctness of the IP address. How to judge the IP address type.

Determine the area by IP address. Determine the correctness of the IP address. How to judge the correctness of the IP address. How to judge the IP address type.

WBOY
WBOYOriginal
2016-07-29 08:54:292603browse

<code><span>//获取用户真实IP</span>
    public function get_client_ip() {
        <span>if</span> (<span>getenv</span>(<span>"HTTP_CLIENT_IP"</span>) && strcasecmp(<span>getenv</span>(<span>"HTTP_CLIENT_IP"</span>), <span>"unknown"</span>))
            <span>$ip</span> = <span>getenv</span>(<span>"HTTP_CLIENT_IP"</span>);
        <span>else</span><span>if</span> (<span>getenv</span>(<span>"HTTP_X_FORWARDED_FOR"</span>) && strcasecmp(<span>getenv</span>(<span>"HTTP_X_FORWARDED_FOR"</span>), <span>"unknown"</span>))
            <span>$ip</span> = <span>getenv</span>(<span>"HTTP_X_FORWARDED_FOR"</span>);
        <span>else</span><span>if</span> (<span>getenv</span>(<span>"REMOTE_ADDR"</span>) && strcasecmp(<span>getenv</span>(<span>"REMOTE_ADDR"</span>), <span>"unknown"</span>))
            <span>$ip</span> = <span>getenv</span>(<span>"REMOTE_ADDR"</span>);
        <span>else</span><span>if</span> (isset (<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]) && <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>] && strcasecmp(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>], <span>"unknown"</span>))
            <span>$ip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>];
        <span>else</span><span>$ip</span> = <span>"unknown"</span>;
        <span>return</span> (<span>$ip</span>);
    }

    <span>// 调用get_client_ip方法</span><span>$ip</span> = <span>$this</span>->get_client_ip();
    <span>// 调用淘宝接口</span><span>$url</span> = <span>"http://ip.taobao.com/service/getIpInfo.php?ip="</span>.<span>$ip</span>;
    <span>$dip</span> = file_get_contents(<span>$url</span>);
    <span>$dd</span> = json_decode(<span>$dip</span>,true);
    var_dump(<span>$dd</span>);</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how to determine the area by IP address, including IP address and determination. 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