CoralCode Web programming snippets

15Aug/100

PHP – Get user ip

To get actual user IP address with PHP you have to use the $_SERVER array and get the value of $_SERVER['REMOTE_ADDR'].

Code example:

$ip = $_SERVER['REMOTE_ADDR'];

Note if the user is behind a proxy server then this method will return with the proxy server IP address.
In this case you can use a bit more complex formula like this:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
	$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.