PHP如何获取真实IP地址

2025-01-07 06:21:34
推荐回答(3个)
回答1:

//最简单获取ip地址代码一句实例

$reIP=$_SERVER["REMOTE_ADDR"];
echo $reIP;

//

//php教程获取ip的算法
if(getenv(HTTP_CLIENT_IP)) {
$onlineip = getenv(HTTP_CLIENT_IP);
} elseif(getenv(HTTP_X_FORWARDED_FOR)) {
$onlineip = getenv(HTTP_X_FORWARDED_FOR);
} elseif(getenv(REMOTE_ADDR)) {
$onlineip = getenv(REMOTE_ADDR);
} else {
$onlineip = $HTTP_SERVER_VARS[REMOTE_ADDR];
}
echo $onlineip;

//可以分出内网与外网站ip地址获取程序

function getip_out(){
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER[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 ? $ip : $_SERVER[REMOTE_ADDR]);
}

echo getip_out();

//php获取ip的算法,用了?号表达式来处理

$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
希望会对你有所帮助

回答2:

用这个函数吧function user_realip() {
if (getenv('HTTP_CLIENT_IP'))
{
$ip = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('REMOTE_ADDR'))
{
$ip = getenv('REMOTE_ADDR');
} else
{
$ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
return $ip;
} 愿编程愉快 ^_^

回答3:

if(getenv( "HTTP_CLIENT_IP ")) {
$REMOTE_ADDR = getenv( "HTTP_CLIENT_IP ");
} elseif(getenv( "HTTP_X_FORWARDED_FOR ")) {
$REMOTE_ADDR = getenv( "HTTP_X_FORWARDED_FOR ");
} else {
$REMOTE_ADDR = getenv( "REMOTE_ADDR ");
} 如果有意隐藏,是无法获取的。