简体   繁体   English

使用$ _SERVER [“ REMOTE_ADDR”]获取正确的IP地址

[英]Get correct IP address using $_SERVER[“REMOTE_ADDR”]

I am using following code : 我正在使用以下代码:

echo $_SERVER["REMOTE_ADDR"];

I echos my IP address as 108.162.225.189 which is somewhere in US. 我在美国某地108.162.225.189我的IP地址为108.162.225.189 My actual IP address is 59.179.64.246 . 我的实际IP地址是59.179.64.246 Is it because I am using cloudFlare? 是因为我正在使用cloudFlare吗? CloudFlare also says CloudFlare还说

CloudFlare sits between your visitor and web server. CloudFlare位于您的访客和Web服务器之间。 So, the CloudFlare connecting IP matters only for any programs that read logs directly from your web server (like awstats). 因此,CloudFlare连接IP仅对直接从您的Web服务器读取日志的任何程序(如awstats)重要。

Is CloudFlare causing this problem? CloudFlare是否引起此问题? How can I get the correct IP address? 如何获得正确的IP地址?

Yes, the shown IP (108.162...) is a CloudFlare IP. 是的,显示的IP(108.162 ...)是CloudFlare IP。 But CloudFlare should provide addtional information. 但是CloudFlare应该提供其他信息。 Try this: 尝试这个:

$ip = 
  isset($_SERVER["HTTP_CF_CONNECTING_IP"])?
     $_SERVER["HTTP_CF_CONNECTING_IP"]:
     $_SERVER["REMOTE_ADDR"]
  ;
echo $ip;

Further information: https://support.cloudflare.com/hc/en-us/articles/200170856-How-do-I-restore-original-visitor-IP-with-vBulletin- 更多信息: https : //support.cloudflare.com/hc/en-us/articles/200170856-How-do-I-restore-original-visitor-IP-with-vBulletin-

You normally can't see your address without tricks because of the cloudflare proxy. 由于存在cloudflare代理,因此您通常看不到花招。

But anyway, you can try grabbing it by the following script 但是无论如何,您可以尝试通过以下脚本来抓取它

if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}

Let me know if it works. 让我知道它是否有效。 Source : http://wp2x.com/get-cloudflare-visitor-ips-php/ 来源: http : //wp2x.com/get-cloudflare-visitor-ips-php/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM