php $_SERVER["REMOTE_ADDR"] 给出服务器 IP 而不是访问者 IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4262081/
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
$_SERVER["REMOTE_ADDR"] gives server IP rather than visitor IP
提问by Lincecum
I'm trying to track IP addresses of visitors. When using $_SERVER["REMOTE_ADDR"]
, I get the server's IP address rather than the visitor's. I tried this on multiple machines at multiple locations and they all resulted in the exact same IP. Is there some PHP/server settings that could be affecting this?
我正在尝试跟踪访问者的 IP 地址。使用时$_SERVER["REMOTE_ADDR"]
,我得到的是服务器的 IP 地址,而不是访问者的 IP 地址。我在多个位置的多台机器上尝试了这个,它们都产生了完全相同的 IP。是否有一些 PHP/服务器设置可能会影响这一点?
回答by simshaun
REMOTE_ADDR
can not be trusted.
REMOTE_ADDR
不能信任。
Anyway, try
总之试试
$ipAddress = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
$ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}
Edit: Also, does your server's router have port forwarding enabled? It may be possible that it's messing with the packets.
编辑:另外,您服务器的路由器是否启用了端口转发?它可能会弄乱数据包。
回答by Pekka
When using $_SERVER["REMOTE_ADDR"], I get the server's IP address rather than the visitor's.
使用 $_SERVER["REMOTE_ADDR"] 时,我得到的是服务器的 IP 地址而不是访问者的 IP 地址。
Then something is wrong, or odd, with your configuration.
那么您的配置有问题或奇怪。
Are you using some sort of reverse proxy? In that case, @simshaun's suggestion may work.
Do you have anything else out of the ordinary in your web server config?
Can you show the PHP code you are using?
Can you show what the address looks like. Is it a local one, or a Internet address?
你在使用某种反向代理吗?在这种情况下,@simshaun 的建议可能会奏效。
您的 Web 服务器配置中还有其他不寻常的地方吗?
你能展示你正在使用的 PHP 代码吗?
你能显示地址的样子吗?它是本地地址还是 Internet 地址?
回答by casablanca
$_SERVER['REMOTE_ADDR']
gives the IP address from which the request was sent to the web server. This is typically the visitor's address, but in your case, it sounds like there is some kind of proxy sitting right before the web server that intercepts the requests, hence to the web server it appears as though the requests are originating from there.
$_SERVER['REMOTE_ADDR']
给出将请求发送到 Web 服务器的 IP 地址。这通常是访问者的地址,但在您的情况下,听起来好像有某种代理坐在拦截请求的 Web 服务器之前,因此对于 Web 服务器来说,请求似乎来自那里。
回答by hammady
Look no more for IP addresses not being set in the expected header. Just do the following to inspect the whole server variables and figure out which one is suitable for your case:
不要再查找未在预期标头中设置的 IP 地址。只需执行以下操作即可检查整个服务器变量并确定哪一个适合您的情况:
print_r($_SERVER);
回答by chiappa
Replacing:
更换:
$_SERVER["REMOTE_ADDR"];
With:
和:
$_SERVER["HTTP_X_REAL_IP"];
Worked for me.
为我工作。
回答by Cosmo Arun
PHP 7.0 $_SERVER varibales have changed. var_dump it and see how it fits your reqs.
PHP 7.0 $_SERVER 变量已更改。var_dump 并查看它如何满足您的要求。
some of them giving remote details are, REMOTE_ADDR HTTP_X_FORWARDED_FOR HTTP_CF_CONNECTING_IP HTTP_CF_IPCOUNTRY
其中一些提供远程详细信息是, REMOTE_ADDR HTTP_X_FORWARDED_FOR HTTP_CF_CONNECTING_IP HTTP_CF_IPCOUNTRY
回答by Sanu
Try this
尝试这个
$_SERVER['HTTP_CF_CONNECTING_IP'];
instead of
代替
$_SERVER["REMOTE_ADDR"];