Home >Backend Development >PHP Tutorial >Unable to get ip address How to get accurate IP address in PHP
<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
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.