How to retrieve IP address in PHP

How To Get IP Address of User in PHP:-

There are many reasons for obtaining IP address of the system for logging, targeting, redirecting, etc. IP information can be found in the $_SERVER array of the respective system. The easiest and most simplest way to get the visitor’s IP address is by reading the REMOTE_ADDR field within this $_SERVER array.

$userip = $_SERVER['REMOTE_ADDR'];  // Get the client ip address

PHP get IP Behind proxy

Sometimes REMOTE_ADDR does not returns the correct IP address of the user. The reason behind this is the use Proxy. In that situation, use the following code to get real IP address of user in PHP.

function GetUserIpAddress(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
echo 'User Real IP - '.GetUserIpAddress();

How To Get Client Ip Address in PHP

The simplest way to get the visitor’s/client’s IP address is using the $_SERVER[‘REMOTE_ADDR’] or $_SERVER[‘REMOTE_HOST’] variables. However this does not return the correct IP address of the visitor if there are prxoy, firewall, VPN or any other software to redirect IP, so we can use some other server variables to get the IP address. The below both functions are equivalent with the difference only in how and from where the values are retrieved.

You can use any of the following 2 functions. Both the functions are equivalent with the difference only where and how they are run to get the specific values. 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 respectively(e.g. apache).

getenv() is used to get the value of an environment variable in PHP

// 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;
}

$_SERVER is an array that contains server variables created by the web server.

// 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;
}

How To Get System Ip Address In PHP

PHP has in built function to detect remote browser, IP address, and other various properties. You can use any one of the following statement to obtain the corresponding IP address from the system.

PHP Syntax to find System IP address

The Syntax is:

$ip = $_SERVER['REMOTE_ADDR'];

OR

$ip= $REMOTE_ADDR;

OR

$ip = $_SERVER['REMOTE_ADDR'];

OR

$ip = getenv('HTTP_CLIENT_IP');

PHP get Remote Host Name:-

The Syntax is:

$ip = $_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($ip);

OR

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
With these codes one can easily find the IP address of the respective user, system and many more with precise solution!

Also read our special section on PHP Interview Questions and Answers now!


You may also like...

Leave a Reply

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