I've tried to get the IP address of client browsing the site using
$_SERVER['REMOTE_ADDR']
, but I'm not getting the exact IP of the client
please help me... Thanks

- 7,955
- 13
- 49
- 92

- 251
- 2
- 6
- 13
-
2Actually this is the way to do it. What do you mean by "not the exact IP"? – JochenJung Aug 10 '10 at 11:45
-
What do you get from REMOTE_ADDR? – fabrik Aug 10 '10 at 11:45
-
3i suspsect you are getting the external ip from the router - not the internal IP address you are expecting of the machine viewing the site – Alex Aug 10 '10 at 11:46
-
There are an enormous number of reasons you might not be getting the results you expect, any, all, or none of which may apply to you. We'd need to know details about how your application is deployed, such as what HTTP server is being used, whether there are any caching or proxying mechanisms in use, etc. – Nicholas Knight Aug 10 '10 at 11:46
-
my client ip is 10.173.88.22 but i get 164.100.6.102..this ip... – user389055 Aug 10 '10 at 11:46
-
@user389055: What would be the advantage of knowing the local IP address of the client? – Gumbo Aug 10 '10 at 11:48
4 Answers
$_SERVER['REMOTE_ADDR']
is the best you will get really.
There are various other headers that can be sent by the client (HTTP_FORWARDED_FOR
et al.) - see this question for a complete overview - but these can be freely manipulated by the client and are not to be deemed reliable.
// Get the real client IP
function getIP() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
$ip = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$ip = getenv("REMOTE_ADDR");
} else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = "unknown";
}
return($ip);
}

- 399
- 1
- 5
- 20
The IP address that the server sees is the client's public-facing IP address. If they're behind a NAT router, they will have a different address inside their network.
If you run ipconfig
(Windows) or ifconfig
(Unix-y systems) on the client machine, you'll get their local IP address. If it's in the 192.168.x.x or 10.x.x.x ranges, they're behind a NAT router and the internet will see them coming from a different address.

- 26,428
- 5
- 49
- 48
If user is behind a proxy you will be getting the IP of the proxy. The user IP would be then either one of these (you'd need to check both): $_SERVER['HTTP_CLIENT_IP'] $_SERVER['HTTP_X_FORWARDED_FOR']
If any of them is set, then user is behind a proxy (unless he is faking those headers) and you should use them as the source IP. Else use $_SERVER['REMOTE_ADDR'].

- 1,779
- 12
- 19