Home >Backend Development >PHP Tutorial >Unable to get ip address How to get accurate IP address in PHP

Unable to get ip address How to get accurate IP address in PHP

WBOY
WBOYOriginal
2016-07-28 08:30:151072browse

<code><span>// 获取IP地址</span>
function GetIP() {
    <span>if</span>(<span>getenv</span>(<span>'HTTP_CLIENT_IP'</span>) && strcasecmp(<span>getenv</span>(<span>'HTTP_CLIENT_IP'</span>), <span>'unknown'</span>)) {
        <span>$thisip</span> = <span>getenv</span>(<span>'HTTP_CLIENT_IP'</span>);
    } elseif(<span>getenv</span>(<span>'HTTP_X_FORWARDED_FOR'</span>) && strcasecmp(<span>getenv</span>(<span>'HTTP_X_FORWARDED_FOR'</span>), <span>'unknown'</span>)) {
        <span>$thisip</span> = <span>getenv</span>(<span>'HTTP_X_FORWARDED_FOR'</span>);
    } elseif(<span>getenv</span>(<span>'REMOTE_ADDR'</span>) && strcasecmp(<span>getenv</span>(<span>'REMOTE_ADDR'</span>), <span>'unknown'</span>)) {
        <span>$thisip</span> = <span>getenv</span>(<span>'REMOTE_ADDR'</span>);
    } elseif(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>$thisip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>];
    }

    preg_match(<span>"/[\d\.]{7,15}/"</span>, <span>$thisip</span>, <span>$thisips</span>);
    <span>$thisip</span> = <span>$thisips</span>[<span>0</span>] ? <span>$thisips</span>[<span>0</span>] : <span>'unknown'</span>;
    <span>return</span><span>$thisip</span>;
}</code>

Has been tested. You can get the real IP address! TCP/IPv4
 PHP获取准确的IP地址的方法
 PHP获取准确的IP地址的方法

can be used directly in the code. You're welcome. Please call me ‘Lei Feng’

').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 obtain an accurate IP address using PHP, including the inability to obtain an IP address. 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