Get correct IP address using $_SERVER["REMOTE_ADDR"]

4.8k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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-

0
On

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/