-1

I know this question was asked by many times, but I did not get the correct answer from my side. Here I am using the following code to get the end user system IP address from my hosted website.

function ipCheck() {
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
echo ipCheck();

In my local network this code is working fine. It correctly gave my local IP 192.168.xxx.xxx. but when I run this code on my website, it gave some other ip address.

Patrick Geyer
  • 1,515
  • 13
  • 30
  • 4
    If you want truly reliable detection, this code is useless. `$_SERVER['REMOTE_ADDR']` is the only address that can not be forged by the client. Re your problem - that is by design: your web site can not see your local IP address. Whether routers add a header like `HTTP_X_FORWARDED_FOR` is up to them, and they usually won't. – Pekka Sep 17 '13 at 13:41
  • You cannot an internal IP address because of NAT. You can only get their visible external IP. – aynber Sep 17 '13 at 13:41
  • You can only reliably get the address of the last machine in the chain. This may happen to be the client's IP address, but can also be a proxy. Local addresses are meaningless (and invisible) from outside the LAN. – George Brighton Sep 17 '13 at 13:42
  • try this answer - http://stackoverflow.com/questions/3219178/php-how-to-get-local-ip-of-system/23077979#23077979 – Rohit Suthar Apr 15 '14 at 08:12

2 Answers2

2

It is working correctly. The IP address it will be giving you will be your external IP address (go to google and search "what is my ip address" and it should show you what your external IP is so you can verify your code is correct if you wish).

When used on a server hosted outside of your local network, this WILL NOT show your local ip address of 192.168.x.x as that information is not provided in the request. That IP address is only valid on your local network.

Lee
  • 10,496
  • 4
  • 37
  • 45
1

You can not do this because of usual Network Address Translating in IPv4 networks. That means all IP addresses behind NAT will be visible as a single IP-address (and that will be gateway address) and, therefore, you will only able to get gateway address.

Besides this, there are Proxy Servers, that could hide or change real client (i.e. his gateway) address. So in common case your question has negative answer, i.e. it's impossible.

And remember, HTTP_X_FORWARDED_FOR is a non-mandatory HTTP-request header, so it can be sent or not. It can be anything (with something like LiveHTTPHeaders under Firefox I can send 'Hello, world!' in this header and script will see that).

Alma Do
  • 37,009
  • 9
  • 76
  • 105