Home > Backend Development > PHP Tutorial > How to get the real IP of the client? NOTIC: [8] Undefined variable: ip How to correct this error?

How to get the real IP of the client? NOTIC: [8] Undefined variable: ip How to correct this error?

WBOY
Release: 2016-08-20 09:04:00
Original
1352 people have browsed it

HTTP_CLIENT_IP
HTTP_X_FORWARD_IP
REMOTE_ADDR

Detect in sequence? Or is there any other way? Except IP138?

Looking at the following writing using the strictestgrammar, what’s wrong with it? How can I change it?

<code>    function Getip() {
        
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {//获取代理ip
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        }

        if ($ip) {
            $ips = array_unshift($ips, $ip);
        }
        $count = count($ips);
        for ($i = 0; $i < $count; $i++) {
            if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) {//排除局域网ip
                $ip = $ips[$i];
                break;
            }
        }
        $tip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
        if ($tip == "127.0.0.1") {//获得本地真实IP
            return $this -> get_onlineip();
        } else {
            return $tip;
        }

    }</code>
Copy after login
Copy after login

Or use the following function?

<code>// 定义一个函数getIP() 
function getIP() 
{ 
global $ip; 

if (getenv("HTTP_CLIENT_IP")) 
$ip = getenv("HTTP_CLIENT_IP"); 
else if(getenv("HTTP_X_FORWARDED_FOR")) 
$ip = getenv("HTTP_X_FORWARDED_FOR"); 
else if(getenv("REMOTE_ADDR")) 
$ip = getenv("REMOTE_ADDR"); 
else 
$ip = "Unknow"; 

return $ip; 
} 

</code>
Copy after login
Copy after login

Reply content:

HTTP_CLIENT_IP
HTTP_X_FORWARD_IP
REMOTE_ADDR

Detect in sequence? Or is there any other way? Except IP138?

Looking at the following writing using the strictestgrammar, what’s wrong with it? How can I change it?

<code>    function Getip() {
        
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {//获取代理ip
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        }

        if ($ip) {
            $ips = array_unshift($ips, $ip);
        }
        $count = count($ips);
        for ($i = 0; $i < $count; $i++) {
            if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) {//排除局域网ip
                $ip = $ips[$i];
                break;
            }
        }
        $tip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
        if ($tip == "127.0.0.1") {//获得本地真实IP
            return $this -> get_onlineip();
        } else {
            return $tip;
        }

    }</code>
Copy after login
Copy after login

Or use the following function?

<code>// 定义一个函数getIP() 
function getIP() 
{ 
global $ip; 

if (getenv("HTTP_CLIENT_IP")) 
$ip = getenv("HTTP_CLIENT_IP"); 
else if(getenv("HTTP_X_FORWARDED_FOR")) 
$ip = getenv("HTTP_X_FORWARDED_FOR"); 
else if(getenv("REMOTE_ADDR")) 
$ip = getenv("REMOTE_ADDR"); 
else 
$ip = "Unknow"; 

return $ip; 
} 

</code>
Copy after login
Copy after login

At present, this method is the safest.
ip138 can only get the visitor’s IP. If it is a server access, it is the server’s IP

At present, there is not much research on this requirement. In normal work, it is obtained by sequential detection.

In fact, ordinary users will not deliberately hide their IP. Nor will they access your service through several layers of proxies. So it is enough to judge these three in order. For ordinary users, these three can be obtained The authenticity of the IP is also quite high.

This is a good question. This is not something that can be easily solved. Some client IPs have problems with deception and proxying, so it is not 100% sure to obtain them. However, as other netizens said, there are still very few ordinary users using proxies. I believe you can use your own method, or you can refer to other third-party libraries for obtaining IPs

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template