phpremote-client

How can I get the client IP using PHP?


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.


Solution

  • 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