PHP Basics

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;
}
Back to top

Fastest WordPress Hosting With WPEngine

Stunning speed, powerful security, and best-in-class customer service. At WP Engine.

Risk free for 60 days