获取用户 IP 地址的最终方法

可能的复制品:
函数获取用户 ip 地址

<?PHP


$ipaddress = $_SERVER["REMOTE_ADDR"];


echo "Your IP is $ipaddress!";


?>

有人告诉我这种获取 IP 地址的方法存在一些问题,比如能够欺骗。有没有更好的方法来收集 IP 地址?寻找获得 IP 地址的更好方法的教程?

89805 次浏览

$_SERVER['REMOTE_ADDR'] is the only reliable IP address you'll get - it's extracted directly from the TCP stack and is where the current connection was established from. This means if the user is connecting via a proxy, you'll get the proxy's address, not the user's.

Any of the other header-based ones are unreliable, as HTTP headers are trivial to forge. You can use the information from them, if you'd like, as long as you don't TRUST it.