use $_SERVER['REMOTE_ADDR'];
An example
$userIP = $_SERVER['REMOTE_ADDR'];
use $_SERVER['REMOTE_ADDR'];
An example
$userIP = $_SERVER['REMOTE_ADDR'];
$_SERVER['REMOTE_ADDR']
'REMOTE_ADDR' - The IP address from which the user is viewing the current page.
$_SERVER['REMOTE_HOST']
'REMOTE_HOST' - The Host name from which the user is viewing the current page. The reverse dns lookup is based off the REMOTE_ADDR of the user.
Note: Your web server must be configured to create this variable. For example in Apache you'll need HostnameLookups On inside httpd.conf for it to exist.
$_SERVER['REMOTE_PORT']
'REMOTE_PORT' - The port being used on the user's machine to communicate with the web server.
It is often as easy as getting the REMOTE_ADDR
value from the $_SERVER
but sometimes when the user is behind a proxy server, that value will be that of the proxy server, not the user. A "nice" proxy server would put the user's IP in other headers, such as HTTP_CLIENT_IP
or X_FORWARDED_FOR
.
However, it should be mentioned that there is probably no perfect way of getting the client's IP. The method below would consider if the user is behind a proxy.
if(array_key_exists("HTTP_CLIENT_IP",$_SERVER)
&& !empty($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
else if(array_key_exists("X_FORWARDED_FOR",$_SERVER)
&& !empty($_SERVER["X_FORWARDED_FOR"])) {
return $_SERVER["X_FORWARDED_FOR"];
}
else {
return = $_SERVER["REMOTE_ADDR"];
}