I've found two approaches to gives me user's IP:
Approach1 :
function get_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ip = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ip = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ip = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ip = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ip = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ip = getenv('REMOTE_ADDR');
else
$ip = 'UNKNOWN';
return $ip;
}
Aproach2 :
function get_ip() {
if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
May you please tell me which one is better? Actually I need to execute that function get_ip()
per each page loading. Also as you see, the first approach has a lot of conditions (if
-statements). So Can I simply use the second one?
In other word, is there advantage from approach1 than approach2?