php 如何跟踪代理背后的真实 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5349604/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:06:32  来源:igfitidea点击:

How To Track the Real IP Address Behind the Proxy

php

提问by kedomonzter

How can we track the Real IP address behind the proxy using PHP?I mean a pure PHP implementation.Because they can turn off the js of their browsers.BTW when the JS is turn on I may use HTML 5 geolocation so I don't need the IP address to locate the user.

我们如何使用 PHP 跟踪代理背后的真实 IP 地址?我的意思是纯 PHP 实现。因为他们可以关闭浏览器的 js。顺便说一句,当 JS 打开时,我可能会使用 HTML 5 地理定位,所以我不需要IP地址来定位用户。

回答by Quentin

You can look at the X-Forwarded-ForHTTP header if one is sent, but there is no guaranteed way to know.

如果发送了X-Forwarded-ForHTTP 标头,您可以查看X-Forwarded-ForHTTP 标头,但不能保证知道的方法。

回答by Cogicero

This PHP code will work, and is useful for forward and reverse proxies. For reverse proxies, note that the X_FORWARDED_FOR header information, if available at all, can be forged and should not be trusted at all.

此 PHP 代码将起作用,并且对于正向和反向代理很有用。对于反向代理,请注意 X_FORWARDED_FOR 标头信息(如果可用)可以伪造并且根本不应该信任。

if($_SERVER["HTTP_X_FORWARDED_FOR"] != ""){
   $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
   $proxy = $_SERVER["REMOTE_ADDR"];
   $host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}else{
   $IP = $_SERVER["REMOTE_ADDR"];
   $proxy = "No proxy detected";
   $host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
}

回答by Marcin

The user who is behind a proxy does not have a globally routable IP address. If you want to maintain session state, you're better off having a session key you generate, and setting it as a cookie, or keeping it in the URL (there are tradeoffs with that approach).

代理背后的用户没有全球可路由的 IP 地址。如果您想保持会话状态,最好拥有生成的会话密钥,并将其设置为 cookie,或将其保存在 URL 中(这种方法需要权衡)。

If you really want to find out what their IP address is on their own network, you could probably use javascript in the page to find it out, and then send it back to your server.

如果您真的想找出他们自己网络上的 IP 地址,您可以在页面中使用 javascript 来查找,然后将其发送回您的服务器。