Home >Backend Development >PHP Tutorial >How to get the real IP of the client? NOTIC: [8] Undefined variable: ip How to correct this error?
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>
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>
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>
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>
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