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.
Hi Mukesh Jakhar. Can I please ask for the script please. I would like to use it on my website. Thank you. Richard
Ya sure… just check your inbox i sent you a mail read out and let me know..
Could you please give us the source code that would be easier for us to understand neither to implement.
Thanks,
Best Regards,
thanks to rply… i will try to send code as possible soon