简体   繁体   中英

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. My actual IP address is 59.179.64.246 . Is it because I am using cloudFlare? CloudFlare also says

CloudFlare sits between your visitor and web server. So, the CloudFlare connecting IP matters only for any programs that read logs directly from your web server (like awstats).

Is CloudFlare causing this problem? How can I get the correct IP address?

Yes, the shown IP (108.162...) is a CloudFlare IP. But CloudFlare should provide addtional information. 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-

You normally can't see your address without tricks because of the cloudflare proxy.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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