Firstly let me clarify a few things up.
When your on localhost your not using your ISP To fetch a webpage, thus you would use an internal ip of 127.0.0.1
or ::1
for ipv6.
If your fetching the page from over a local network via a router of some kind, you will have an ip assigned by the router such as 192.168.1.90
if your site is hosted outside the network then you ask your ISP to fetch the site for you, meaning you get use the IP specified by whatsmyip.
if your using a DNS Server such as Opendns then your asking your ISP to ask Opendns to fetch the site for you, and open dns uses a set of ip's that are different to yours for obvious reasons.
there may be some sort of proxy that may be interfering, so what you should do is counter for that, a standard proxy site should forward the clients IP on to the server incase of any direct connections required and for several other reasons.
This being said you can usually find the IP by checking several other params before you check REMOTE_ADDR
, here is a class I have created for one of my projects but you can just take what you need:
https://github.com/AdminSpot/ASDDL/blob/master/system/classes/http/request.php
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
{
if (array_key_exists($key, $_SERVER) === true)
{
foreach (explode(',', $_SERVER[$key]) as $ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP) !== false)
{
$this->ip = $ip;
break;
}
}
}
}
As you can see the order of the array is very important:
- HTTP_CLIENT_IP
- HTTP_X_FORWARDED_FOR
- HTTP_X_FORWARDED
- HTTP_X_CLUSTER_CLIENT_IP
- HTTP_FORWARDED_FOR
- HTTP_FORWARDED
- REMOTE_ADDR
notice the REMOTE_ADDR
comes last, this is because this is the last resort and most of the time is incorrect.