您的位置:首页 - 教程 - PHP - 正文
php获取真实内网IP方法
function getIP() {
if (isset($_SERVER)) {
if (isset($_SERVER["HTTP_CDN_SRC_IP"])) {
$realip = $_SERVER["HTTP_CDN_SRC_IP"];
} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv("HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
//如果是代理服务器,有可能返回两个IP,这是取第一个即可
if (stristr($realip, ','))
$realip = strstr($realip, ',', true);
return (str_replace('#', '', $realip));
}

评论: