Retrieve visitor IP

Scenario:

Webmaster always wish to retrieve vistors’ information for their own report and review. But in some situation you might getting the incorrect information.
Today I would like to share my recent experience on the visitor IP.

As we know, it is possible to retrieve visitor IP in the $_SERVER[‘REMOTE_ADDR’] parameter.
But in certain scenarios, you are getting dummy data:

  1. Using Proxy
  2. Website behind a CDN (Content Delivery Network, like Cloudflare) service


Solution:

For above scenarios, $_SERVER[‘REMOTE_ADDR’] will provide us the proxy or CDN IP instead of the actual visitor IP.
Below is the additional information in $_SERVER variable that we can retrieve the actual visitors’ IP.

$ip = $_SERVER['REMOTE_ADDR'];
if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

Leave a Reply