How to detect the correct IP address

This code snippet handles up to four modes of IP detection, so you can be sure that you can very well filter out the correct IP address for your application needs.

function getip() {
if (isset ($_SERVER[‘HTTP_X_FORWARDED_FOR’])) {
$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
}
else
if (isset ($_SERVER[‘HTTP_REMOTE_ADDR_REAL’])) {


$ip = $_SERVER[‘HTTP_REMOTE_ADDR_REAL’];
}
else
if (isset ($_SERVER[‘HTTP_CLIENT_IP’])) {
$ip = $_SERVER[‘HTTP_CLIENT_IP’];
}
else {
$ip = $_SERVER[‘REMOTE_ADDR’];
}
return $ip;
}

Leave a Reply

Your email address will not be published. Required fields are marked *