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. ).
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();
$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);
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.
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.