0

I have a webpage with PHP and im trying to get the client IP, but what I get is the server GATEWAY ip address (192.168.0.1).

This is what I have at the office:

ISP ---> ISP router ---> My router(192.168.0.1) ----> (192.168.0.2)My server.

I tryed all this lines, but I wasnt able to get the real ip, only SERVER GATEWAY ip.

$_SERVER['HTTP_X_FORWARDED_FOR']
$_SERVER['HTTP_CLIENT_IP']
$_SERVER['HTTP_X_REAL_IP']
$_SERVER['GATEWAY_INTERFACE']
$_SERVER['SERVER_ADDR']
$_SERVER['SERVER_NAME']
$_SERVER['SERVER_SOFTWARE']
$_SERVER['SERVER_PROTOCOL']
$_SERVER['REQUEST_METHOD']
$_SERVER['REQUEST_TIME']
$_SERVER['QUERY_STRING']
$_SERVER['DOCUMENT_ROOT']
$_SERVER['HTTP_ACCEPT']
$_SERVER['HTTP_ACCEPT_CHARSET']
$_SERVER['HTTP_ACCEPT_ENCODING']
$_SERVER['HTTP_ACCEPT_LANGUAGE']
$_SERVER['HTTP_CONNECTION']
$_SERVER['HTTP_HOST']
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_USER_AGENT']
$_SERVER['HTTPS']
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_HOST']
$_SERVER['REMOTE_PORT']
$_SERVER['SCRIPT_FILENAME']
$_SERVER['SERVER_ADMIN']
$_SERVER['SERVER_PORT'] 
$_SERVER['SERVER_SIGNATURE']
$_SERVER['PATH_TRANSLATED']
$_SERVER['SCRIPT_NAME']
$_SERVER['REQUEST_URI']
$_SERVER['PHP_AUTH_DIGEST']
$_SERVER['PHP_AUTH_USER'] 
$_SERVER['PHP_AUTH_PW']
$_SERVER['AUTH_TYPE']

The following variables are "undefined":

-HTTP_X_FORWARDED_FOR
-HTTP_CLIENT_IP
-HTTP_X_REAL_IP
-HTTPS
-REMOTE_HOST
-PATH_TRANSLATED
-PHP_AUTH_DIGEST
-PHP_AUTH_USER
-PHP_AUTH_PW
-AUTH_TYPE



EDIT: tried "var_dump($_SERVER)" and cannot find the client IP address :(

EDIT 2: I was googling a bit, and I "found the answer". What the user Martin Hohenberg said is right. In the NAT proccess the Headers are destroyed so my server is unable to read the original IP address. At the moment I dont know a posible solution to this problem without moving the server to the front.

Redder
  • 146
  • 1
  • 5
  • 17

2 Answers2

2

Similar question

Try

echo $_SERVER['REMOTE_ADDR'];

Read more

function getUserIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) //if from shared
    {
        return $_SERVER['HTTP_CLIENT_IP'];
    }
    else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //if from a proxy
    {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        return $_SERVER['REMOTE_ADDR'];
    }
}

Please check the read more link and the Question link

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243
  • as I said, that returns server gateway IP, not the client IP – Redder May 15 '13 at 11:46
  • 3
    @Redder that hints that your server is behind some sort of NAT - which means that the TCP header is getting rewritten, essentially loosing the original client's IP – ty812 May 15 '13 at 11:48
  • @Dasun - I tried all those server variables, and I have the same problem. – Redder May 15 '13 at 11:53
  • 1
    @MartinHohenberg - As far as I know, every router does NAT to translate public netowrk to private network. So why this works in other's networks and not in this one? Maybe a router conf problem?? – Redder May 15 '13 at 11:58
2

If you are making a request from the machine where the server is running, then $_SERVER['REMOTE_ADDR'] will obviously contain your own IP address, which is 192.168.0.1

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Jugo
  • 66
  • 8