I've tried to get the IP address of client开发者_JS百科 browsing the site using
$_SERVER['REMOTE_ADDR']
, but I'm not getting the exact IP of the client
please help me... Thanks
$_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);
}
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.
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'].
精彩评论