Get IP Address (PHP)


To totally unlock this section you need to Log-in


Login

Nowadays, getting a visitor's IP address is very important for many reasons. These reasons can include logging, targeting, redirecting, etc. IP information can be found in the $_SERVER array. The easiest way to get the visitor's IP address is by reading the REMOTE_ADDR field within this $_SERVER array. The following example illustrates how easy it is:

<?php $ip = $_SERVER['REMOTE_ADDR']; ?>

This solution, however, is not entirely accurate due to proxy applications that users may have, meaning that you will get the IP of the proxy server and not the real user address. Luckily, PHP gives us a few more indices to get more accurate results.

Proxy servers extend the HTTP header with new property which stores the original IP. In order to be more accurate with your reading, you would need to verify all three of the following indices (including the first one we gave above). The other two are the following:

<?php 
    $remote = $_SERVER['REMOTE_ADDR'];

    $forwarded = $_SERVER['HTTP_X_FORWARDED_FOR'];

    $client_ip = $_SERVER['HTTP_CLIENT_IP'];
?>

Now that we know how to get the IP address from a user, then we can go ahead and create a reusable function that will indeed return an IP address if it is present, or return false otherwise:

<?php 
    function getIp(){

        $ip = $_SERVER['REMOTE_ADDR'];     
        if($ip){
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
            return $ip;
        }
        // There might not be any data
        return false;
    }
?>

Note: Even with more indices to play around with, it does not mean that this will still be 100% accurate. It is easy to manipulate these values using browser plug-ins to change header information, proxy applications or even proxy websites. Because of this, we recommend you use more information from the user, such as MAC address and the use of cookies.

Other two functions to use

You can use any of the following 2 functions. Both the functions are equivalent with the difference only in how and from where the values are retrieved. The first one getenv() is used to get the values from PHP’s environment variables and the second one $_SERVER is used to get the values from the web server (e.g. apache).

// Function to get the client ip address
function get_client_ip_env() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
 
    return $ipaddress;
}

// Function to get the client ip address
function get_client_ip_server() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
 
    return $ipaddress;
}

  • getenv() is used to get the value of an environment variable in PHP.
  • $_SERVER is an array contains server variables created by the web server.

Note: HTTP_X_FORWARDED_FOR sometimes returns internal or local IP address, which is not usually useful. Also, it would return a comma separated list if it was forwarded from multiple ipaddresses.