醉菜鸟 2021-07-09 19:49:02

PHP获取IP地址

/**
* @return mixed|string
*/
function get_ip(){
$ip = false;
if(!empty($_SESSION["HTTP_CLIENT_IP"])){
$ip = $_SESSION["HTTP_CLIENT_IP"];
}
if(!empty($_SESSION["HTTP_X_FORWARDED_FOR"])){
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return $ip ?: $_SERVER['REMOTE_ADDR'];
}
意见反馈