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'];
    else
        $ipaddress = 'UNKNOWN';
   
}
get_client_ip();

Get user location detail

$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$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.
print_r($geo);

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("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$User_Latitude = $geo["geoplugin_latitude"];
$User_Longitude = $geo["geoplugin_longitude"];

Step 3.

$url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%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.

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

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.