PHP

How to detect/get the real client IP address of website visitors?

Some times we need the IP address of website visitors to fined the visitors locations, spam validation and for some other reasons. So, you can get the IP address by using $_SERVER[‘REMOTE_ADDR’], but this syntax not always return real IP address of the visitor. Because the user may be behind the Proxy Server. So, REMOTE_ADDR function may not provide the real IP address of the visitors.

So, to get the real client IP address you can use below codes.

// Function to get the client ip address

function get_client_ip() {
$ip_address = '';

if ($_SERVER['HTTP_CLIENT_IP'])
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
 else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
 else if($_SERVER['HTTP_X_FORWARDED'])
    $ip_address = $_SERVER['HTTP_X_FORWARDED'];
 else if($_SERVER['HTTP_FORWARDED_FOR'])
    $ip_address = $_SERVER['HTTP_FORWARDED_FOR'];
 else if($_SERVER['HTTP_FORWARDED'])
    $ip_address = $_SERVER['HTTP_FORWARDED'];
 else if($_SERVER['REMOTE_ADDR'])
    $ip_address = $_SERVER['REMOTE_ADDR'];
 else
    $ip_address = 'Unknown IP Address';
return $ip_address;
}

Please fell free to discuss with me regarding this issue.

Comment here