如果用户使用了代理服务器,真实ip就不在HTTP_CLIENT_IP头中,而需要通过http头HTTP_X_FORWARDED_FOR来解析了。 如下php函数:
- function GetIP() { //获取IP
- if ($_SERVER["HTTP_X_FORWARDED_FOR"])
- $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- else if ($_SERVER["HTTP_CLIENT_IP"])
- $ip = $_SERVER["HTTP_CLIENT_IP"];
- else if ($_SERVER["REMOTE_ADDR"])
- $ip = $_SERVER["REMOTE_ADDR"];
- else if (getenv("HTTP_X_FORWARDED_FOR"))
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- else if (getenv("HTTP_CLIENT_IP"))
- $ip = getenv("HTTP_CLIENT_IP");
- else if (getenv("REMOTE_ADDR"))
- $ip = getenv("REMOTE_ADDR");
- else
- $ip = "Unknown";
- return $ip;
- }
- ?>
复制代码
|