php获取客户端IP地址的几种方法

2025-04-04 09:00:42
推荐回答(1个)
回答1:

就这一种吧:$_SERVER['REMOTE_ADDR']
完善点的可以这样做:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}