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.

4 thoughts on “How to track website visitors location in php”

Leave a Reply

Related Posts: You may like

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

Convert Base64 string to an image file

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.