How to track website visitors location in php

How to track website visitors location in php

Many times you have need to track the your website visitor’s location as per your web project requirement. In this tutorial we will show you the simplest way to getting current location of the user. Using only PHP, you can easily track your website visitor’s country, state, city, location, and zipcode and much more.

The helping of geoplugin API, just you have need get the visitor’s IP Address than you can get the full detail of a visitor such as ( Country Name, City, Region, Currency Symbol, Currency Code, Latitude, Longitude etc. ).

Get The IP Address of visitor’s

Here we are creating a function for get visitor’s IP Address, but you can also get using php function ( $user_ip = getenv(‘REMOTE_ADDR’); ).

function get_client_ip() {
    global $ipaddress;
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
        $ipaddress = 'UNKNOWN';

Get user location detail

$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("$user_ip"));
echo $User_Ipaddress=$geo["geoplugin_request"];
echo $User_Country = $geo["geoplugin_countryName"];
echo $User_City = $geo["geoplugin_city"];
echo $User_Region = $geo["geoplugin_region"];
echo $User_CurrencySymbol = $geo["geoplugin_currencySymbol"];
echo $User_CurrencyCode = $geo["geoplugin_currencyCode"];
echo $User_Latitude = $geo["geoplugin_latitude"];
echo $User_Longitude = $geo["geoplugin_longitude"];

//For More Detail about your visitor simply use that.

Track website visitor’s location using google api

If you want to track your visitor’s location details like Full address of visitor’s, city, state, country, zip-code etc then you can use the following script. We can easily get the visitors/ user IP address by using PHP and google api.

Step 1.
Get the IP Address of the visitor’s.

$user_ip = getenv('REMOTE_ADDR');

Step 2.
Get the Latitude and longitude of user using server side scripting language (PHP).

$geo = unserialize(file_get_contents("$user_ip"));
$User_Latitude = $geo["geoplugin_latitude"];
$User_Longitude = $geo["geoplugin_longitude"];

Step 3.

$url = sprintf(",%s", $User_Latitude, $User_Longitude);
$content = file_get_contents($url); // get json content
$metadata = json_decode($content, true); //json decoder
if(count($metadata['results']) > 0)
    $result = $metadata['results'][0];
    $User_location=$result['formatted_address']; // Address into normalize format.
echo $User_location; // This print the full address of user.

I hope this tutorial help of you, Please share with your friends.

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.