Php – How to get real IP of user using PHP?

ipphp

I've been trying to get real IP address of the user, and not a proxy address. For that I've done this:

$ip1 = $_SERVER['REMOTE_ADDR'];
$ip2 = $_SERVER['HTTP_X_FORWARDED_FOR'];
$ip3 = $_SERVER['HTTP_FORWARDED'];
mail("me@domain.com", "Report", "IP1 is $ip1, IP2 is $ip2, IP3 is $ip3 .");

But when a user is using proxy, the above script gets the proxy address and not the real IP address:

IP is [proxy_addr_here], IP2 is  , IP3 is    .

Is there any way to get real IP just like whatismyip.com tells (it tells real IP address, proxy address and useragent)?

Update : Whatismyip tells me this
"Your IP Address Is: [my real IP]
Proxy: [my proxy address]
City: Alipur
State/Region: Delhi
Country: IN –
ISP: Bharti Airtel Ltd."

How come it gathers all the details so accurately but not my PHP script?

Best Solution

You could do

$ip= isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? 
     $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

But even that is not 100%. Getting IP of the real user now a days is not a guarantee with so many NAT firewalls in between, let alone proxies.

Edit

To answer your edit as to how they show you more information, you could use many features of the language to get more request details. For example try

print_r(getallheaders());

Then you can also use

 print_r($_SERVER);

And extract the required information from there.

Edit 2

Even your favorite whatismyip.com reports

Your IP Location can be found using our IP Lookup tool. No IP Lookup tool is 100% accurate due to many different factors. Some of those factors include where the owner of the IP has it registered, where the agency that controls the IP is located, proxies, cellular IPs, etc. If you are in the US and the controlling agency of the IP is located in Canada, chances are the IP address lookup results will show as Canada. Showing a Canadian IP while in the US is very common among Blackberry users on the Verizon network.

Reference

You might still ask ok then why cant I get at least what they show? Im sure they have put up a lot of research and resources in setting up that tool, its not a one line PHP code, in fact there is no telling whether that site is written in PHP at all. Some more research and you will be on your way to try to match their lookup.