我知道有许多$ _SERVER变量头可用于IP地址检索.我想知道如何使用所述变量最准确地检索用户的真实IP地址(很清楚没有方法是完美的)?
我花了一些时间试图找到一个深入的解决方案,并根据许多来源提出了以下代码.我很乐意,如果有人可以请求在答案中挖洞,或者对某些事情做些准确的解释.
编辑包括来自@Alix的优化
/** * Retrieves the best guess of the client's actual IP address. * Takes into account numerous HTTP proxy headers due to variations * in how different ISPs handle IP addresses in headers between hops. */ public function get_ip_address() { // Check for shared internet/ISP IP if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP'])) return $_SERVER['HTTP_CLIENT_IP']; // Check for IPs passing through proxies if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // Check if multiple IP addresses exist in var $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); foreach ($iplist as $ip) { if ($this->validate_ip($ip)) return $ip; } } } if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED'])) return $_SERVER['HTTP_X_FORWARDED']; if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR'])) return $_SERVER['HTTP_FORWARDED_FOR']; if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED'])) return $_SERVER['HTTP_FORWARDED']; // Return unreliable IP address since all else failed return $_SERVER['REMOTE_ADDR']; } /** * Ensures an IP address is both a valid IP address and does not fall within * a private network range. * * @access public * @param string $ip */ public function validate_ip($ip) { if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) return false; self::$ip = $ip; return true; }
REMOTE_ADDR
仍然是最可靠的IP地址来源.$_SERVER
这里提到的其他变量可以很容易地被远程客户端欺骗.此解决方案的目的是尝试确定位于代理后面的客户端的IP地址.出于一般目的,您可以考虑将其与直接返回$_SERVER['REMOTE_ADDR']
并存储两者的IP地址结合使用.
99.9%的用户可以完美地满足您的需求.它不会保护您免受0.1%的恶意用户的攻击,他们希望通过注入自己的请求标头来滥用您的系统.如果依靠IP地址来完成关键任务,那么请求REMOTE_ADDR
并不要为代理背后的人提供服务.
以下是获取IP地址的更简洁,更简洁的方法:
function get_ip_address(){ foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){ if (array_key_exists($key, $_SERVER) === true){ foreach (explode(',', $_SERVER[$key]) as $ip){ $ip = trim($ip); // just to be safe if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){ return $ip; } } } } }
我希望它有所帮助!
您的代码似乎是相当完整了,我看不到任何可能的错误在它(除了通常的知识产权告诫),我会改变的validate_ip()
功能依赖于过滤器扩展,但:
public function validate_ip($ip) { if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) { return false; } self::$ip = sprintf('%u', ip2long($ip)); // you seem to want this return true; }
您的HTTP_X_FORWARDED_FOR
代码段也可以简化:
// check for IPs passing through proxies if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // check if multiple ips exist in var if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) { $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); foreach ($iplist as $ip) { if ($this->validate_ip($ip)) return $ip; } } else { if ($this->validate_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) return $_SERVER['HTTP_X_FORWARDED_FOR']; } }
对此:
// check for IPs passing through proxies if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); foreach ($iplist as $ip) { if ($this->validate_ip($ip)) return $ip; } }
您可能还需要验证IPv6地址.
然而,即便如此,获取用户的真实IP地址也是不可靠的.他们需要做的就是使用一个匿名代理服务器(一个不尊重http_x_forwarded_for,http_forwarded等标头的服务器),你得到的只是他们的代理服务器的IP地址.
然后,您可以查看是否存在匿名的代理服务器IP地址列表,但是无法确保100%准确,并且它所做的最多就是让您知道它是代理服务器.如果有人聪明,他们可以欺骗HTTP转发的标头.
假设我不喜欢当地的大学.我弄清楚他们注册了哪些IP地址,并通过做坏事来获取他们在您网站上禁用的IP地址,因为我发现您尊重HTTP转发.名单是无止境的.
然后,正如您所猜测的那样,内部IP地址,例如我之前提到过的大学网络.很多都使用10.xxx格式.所以你要知道的是,它被转发为共享网络.
然后我不会开始考虑它,但动态IP地址已成为宽带的方式了.所以.即使您获得了用户IP地址,也希望它在2-3个月内更改,最长.
我们用:
/** * Get the customer's IP address. * * @return string */ public function getIpAddress() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { return $_SERVER['HTTP_CLIENT_IP']; } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); return trim($ips[count($ips) - 1]); } else { return $_SERVER['REMOTE_ADDR']; } }
HTTP_X_FORWARDED_FOR的爆炸是因为我们在使用Squid时检测到IP地址的奇怪问题.