If we used $_SERVER[‘REMOTE_ADDR’] to get an IP address, but sometimes it will not return the correct value. If the client is connected with the Internet through Proxy Server then $_SERVER[‘REMOTE_ADDR’] in PHP, it returns the IP address of the proxy server not of the user’s machine.

So here is a simple function in PHP to find the real IP address of the user’s machine.

BY Best Interview Question ON 01 May 2020

Example

function getRealIpAddr()
{
    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;
}