获取用户 ip 地址 php 的权威方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6794782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
definitive way to get user ip address php
提问by user62617
Possible Duplicate:
Function to get user ip address
可能的重复:
获取用户 IP 地址的函数
<?PHP
$ipaddress = $_SERVER["REMOTE_ADDR"];
echo "Your IP is $ipaddress!";
?>
I was told this way of getting ip address has issues such as being able to fool. Is there a better way to gather ip address? looking for tutorials of better way to get ip address?
有人告诉我,这种获取 ip 地址的方式存在诸如能够愚弄之类的问题。有没有更好的方法来收集IP地址?正在寻找获取 IP 地址的更好方法的教程?
回答by Marc B
$_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.
$_SERVER['REMOTE_ADDR']
是您将获得的唯一可靠 IP 地址 - 它直接从 TCP 堆栈中提取,并且是建立当前连接的位置。这意味着如果用户通过代理连接,您将获得代理的地址,而不是用户的地址。
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.
任何其他基于标头的都是不可靠的,因为 HTTP 标头很容易伪造。如果您愿意,您可以使用他们提供的信息,只要您不信任它。