php获取客户端来源的ip,代码如下:
1 function getIP() 2 { 3 if (isset($_SERVER)) { 4 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 5 $realip = $_SERVER['HTTP_X_FORWARDED_FOR']; 6 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { 7 $realip = $_SERVER['HTTP_CLIENT_IP']; 8 } else { 9 $realip = $_SERVER['REMOTE_ADDR']; 10 } 11 } else { 12 if (getenv("HTTP_X_FORWARDED_FOR")) { 13 $realip = getenv( "HTTP_X_FORWARDED_FOR"); 14 } elseif (getenv("HTTP_CLIENT_IP")) { 15 $realip = getenv("HTTP_CLIENT_IP"); 16 } else { 17 $realip = getenv("REMOTE_ADDR"); 18 } 19 } 20 return $realip; 21 } |