获取客户端IP
/** * 获取客户端IP地址 * * @return string */ public static function getClientIp() { if (!empty($_SERVER["HTTP_CLIENT_IP"])) return $_SERVER["HTTP_CLIENT_IP"]; if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $proxy_ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; elseif (($tmp_ip = getenv("HTTP_X_FORWARDED_FOR"))) $proxy_ip = $tmp_ip; else $proxy_ip = ''; if ('' !== $proxy_ip) { if (false === strpos($proxy_ip, ',')) return $proxy_ip; foreach (explode(',', $proxy_ip) as $curr_ip) {// 处理可能有多级代理的情况 if (false === stripos($curr_ip, 'unknown')) $curr_ip = ltrim($curr_ip); else continue; if (0 === strpos($curr_ip, '192.168.')) continue; // 内网IP if (0 === strpos($curr_ip, '10.')) continue; // 内网IP if (0 === strpos($curr_ip, '172.16.')) continue; // 内网IP return $curr_ip; } } if(!empty($_SERVER["REMOTE_ADDR"])) return $_SERVER["REMOTE_ADDR"]; elseif(($retvl = getenv("HTTP_CLIENT_IP"))) return $retvl; elseif(($retvl = getenv("REMOTE_ADDR"))) return $retvl; else return '0.0.0.0'; }