8

Possible Duplicate:
suddenly $_SERVER['REMOTE_ADDR'] is started returning 10.10.10.10 php

I must have missed some fundamental thing here.. But when I navigate to an IP-displaying site such as http://www.whatsmyip.org/ they show a certain IP. But when I echo out $_SERVER["REMOTE_ADDR"] on a page on my site it shows a different IP.

Why is that? And how can I, through PHP, fetch the same IP that the whatsmyip.org site shows?

Community
  • 1
  • 1
Weblurk
  • 6,562
  • 18
  • 64
  • 120
  • What is your server? Which IP exactly do you see? Maybe 127.0.0.1 or 192.168.0.1 or something like these. – OZ_ May 23 '11 at 13:23
  • Is your PHP server running behind an Apache frontend with a rewrite/proxy configuration? – AJ. May 23 '11 at 13:23
  • 1
    @OZ_ The ip I see is not 127.0.0.1 or 192.168.0.1, it's a "normal" IP. – Weblurk May 23 '11 at 13:36

6 Answers6

10

If your computer is on the same network with your server, behind a router with NAT, then you might see your private IP

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
3

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.

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • It may be appropriate to first check if the REMOTE_ADDR is a private address; other values could be spoofed in the header. http://stackoverflow.com/questions/13818064/check-if-an-ip-address-is-private – Mike May 22 '13 at 13:36
2

When your web server is on your local machine it will give you your local IP address mostly 127.0.0.1, that is because you are not accessing it from outside. That is reason.

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
1

What is the IP?

My only tought would be is that you might b behind a proxy. Could it be?

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
  • The "correct" IP from the whatsmyip-site is 213.115.255.236 and ip I get from echoing it out myself is 194.192.151.254 – Weblurk May 23 '11 at 13:42
1

If your server is Apache and you use some front-end before Apache, you can try to use mod_rpaf:
In Debian:
aptitude install libapache2-mod-rpaf
then edit config:
nano /etc/apache2/mods-enabled/rpaf.conf

<IfModule mod_rpaf.c>
RPAFenable On
RPAFsethostname Off
RPAFproxy_ips 127.0.0.1
</IfModule>
OZ_
  • 12,492
  • 7
  • 50
  • 68
0

It could be because you visit it from localhost? Is the IP you see 127.0.0.1, or your local IP (192.168.x.x)?

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46