There are different types of users behind the Internet, so we want to catch the IP address from different portions. Those are:
1. $_SERVER['REMOTE_ADDR']
-
This contains the real IP address of the client. That is the most reliable value you can find from the user.
2. $_SERVER['REMOTE_HOST']
-
This will fetch the host name from which the user is viewing the current page. But for this script to work, hostname lookups on inside httpd.conf must be configured.
3. $_SERVER['HTTP_CLIENT_IP']
-
This will fetch the IP address when the user is from shared Internet services.
4. $_SERVER['HTTP_X_FORWARDED_FOR']
- This will fetch the IP address from the user when he/she is behind the proxy.
So we can use this following combined function to get the real IP address from users who are viewing in diffrent positions,
// Function to get the user IP address
function getUserIP() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}