php $_SERVER['REMOTE_ADDR'] 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4594823/
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
problem with $_SERVER['REMOTE_ADDR']
提问by imez
i used $_SERVER['REMOTE_ADDR'] and it returns client ip address (IP address from which the user is viewing the current page) but at now (and same code) it returns host ip address (i checked ip address with ip location). problem is with host or what? thank u.
我使用了 $_SERVER['REMOTE_ADDR'] 并返回客户端 IP 地址(用户正在查看当前页面的 IP 地址)但现在(和相同的代码)它返回主机 IP 地址(我检查了 ip 地址和 ip 位置) . 问题是主机还是什么?感谢你。
回答by James
You should query for HTTP_X_FORWARDED_FOR
first and if it isn't assigned use REMOTE_ADDR
.
您应该HTTP_X_FORWARDED_FOR
首先查询,如果没有分配 use REMOTE_ADDR
。
回答by Mark Bekkers
@James @imez
@James @imez
By default the client IP is in $_SERVER['REMOTE_ADDR']. When the user enters your site using a PROXY server (HTTP gateway) it tells you who it's proxing for (HTTP_X_FORWARDED_FOR) and will give it's own Proxy IP in $_SERVER['REMOTE_ADDR'].
默认情况下,客户端 IP 位于 $_SERVER['REMOTE_ADDR'] 中。当用户使用代理服务器(HTTP 网关)进入您的站点时,它会告诉您它正在代理谁(HTTP_X_FORWARDED_FOR),并将在 $_SERVER['REMOTE_ADDR'] 中提供它自己的代理 IP。
Anonymous proxies will omit HTTP_X_FORWARDED_FOR or simply lie to you.
匿名代理将省略 HTTP_X_FORWARDED_FOR 或只是对您撒谎。
Knowing you have a real client IP isn't possible.
知道您拥有真实的客户端 IP 是不可能的。
回答by Annika Backstrom
I have to mention that the array key is case-sensitive, and should be upper-case:
我不得不提到数组键是区分大小写的,并且应该是大写的:
var_dump($_SERVER['remote_addr']);
echo "\n";
var_dump($_SERVER['REMOTE_ADDR']);
Output:
输出:
Notice: Undefined index: remote_addr in /home/adam/public_html/2011/01/04/foo.php on line 3
NULL
string(15) "10.0.1.51"
I would var_dump($_SERVER)
just to evaluate the state of your world, and go from there.
我var_dump($_SERVER)
只想评估你的世界的状态,然后从那里开始。