Paulund

Use PHP To Get The Real IP Address

PHP

If you want to record the visitors IP Address there are a few things you need to remember, computers on shared internet connections can have different IP Addresses and visitors coming to your website through a proxy will have different IP addresses.

Computers coming through a proxy, the IP address will come through as the proxy IP address.

Below is a PHP snippet which you can use which will get the real IP Address of the visitor.

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

Paul

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5.

Website: http://www.paulund.co.uk

Share The Wealth

Feedback

Anything to add?