Get Visitor Country, City, Currency Exchange and many more for free Step By Step
You As a Web Developer You may want to get your visitors location data such as visitors country, city, Longitude and Latitude, currency exchange and many more data using his/her IP address only.
You may use this data to improve your web app or to get full anaytic of your users When and from where your visitors come from.
In this article I will show you how to get all of these data using Geoplugin API fro free forever.
You can do this using your prefered programming language but here for this article I will get all the data using PHP.
What is Geoplugin API used for?
Geoplugin API is used to get visitor location information by just looking up on his/her ip address.
So using that API you will get your visitor location information.
Now lets start to get and display all the info from this API
But First you should already know on how to get your visitor IP Address, To get visitor IP Address in PHP You can do like this...
<?php $your_visitor_ip_address = $_SERVER['REMOTE_ADDR']; ?>
Now All starts using Geoplugin URL with your user's ip address at the end.
<?php http://www.geoplugin.net/php.gp?ip='.$your_visitor_ip_address; ?>
Now to get the returned content you need to insert the above URL inside file_get_contents function looks like
<?php $get_data = file_get_contents('http://www.geoplugin.net/php.gp?ip='.$your_visitor_ip_address); ?>
Now you're ready to get and display visitor information. But one thing is left, whats that? Its the unserialize() PHP function,
This unserialize() function is used to converts serialized data back into actual data. So now you must insert $get_data Variable to unserialize() function
<?php $get_all_data_list = unserialize($get_data); ?>
Now lets get all the visitor location info
<?php $country = $get_all_data_list['geoplugin_countryName']; $city = $get_all_data_list['geoplugin_cityName']; $continent = $get_all_data_list['geoplugin_continentName']; $currency_exchange = $get_all_data_list['geoplugin_currencyConverter']; $region = $get_all_data_list['geoplugin_region']; $countryCode = $get_all_data_list['geoplugin_countryCode']; $continentCode = $get_all_data_list['geoplugin_continentCode']; $latitude = $get_all_data_list['geoplugin_latitude']; $longitude = $get_all_data_list['geoplugin_longitude']; $timezone = $get_all_data_list['geoplugin_timezone']; ?>
All in one code
<?php $your_visitor_ip_address = $_SERVER['REMOTE_ADDR']; $get_data = file_get_contents('http://www.geoplugin.net/php.gp?ip='.$your_visitor_ip_address); $get_all_data_list = unserialize($get_data); $country = $get_all_data_list['geoplugin_countryName']; $city = $get_all_data_list['geoplugin_city']; $continent = $get_all_data_list['geoplugin_continentName']; $currency_exchange = $get_all_data_list['geoplugin_currencyConverter']; $region = $get_all_data_list['geoplugin_region']; $regionCode = $get_all_data_list['geoplugin_regionCode']; $regionName = $get_all_data_list['geoplugin_regionName']; $countryCode = $get_all_data_list['geoplugin_countryCode']; $continentCode = $get_all_data_list['geoplugin_continentCode']; $latitude = $get_all_data_list['geoplugin_latitude']; $dmaCode = $get_all_data_list['geoplugin_dmaCode']; $inEU = $get_all_data_list['geoplugin_inEU']; $euVATrate = $get_all_data_list['geoplugin_euVATrate']; $longitude = $get_all_data_list['geoplugin_longitude']; $currencyCode = $get_all_data_list['geoplugin_currencyCode']; $currencySymbol = $get_all_data_list['geoplugin_currencySymbol']; $locationAccuracyRadius = $get_all_data_list['geoplugin_locationAccuracyRadius']; $timezone = $get_all_data_list['geoplugin_timezone']; echo 'Geolocation results for '.$your_visitor_ip_address.':
Region Code: '.$regionCode.'
Region Name: '.$regionName.'
DMA Code: '.$dmaCode.'
Country Name: '.$country.'
Country Code: '.$countryCode.'
Continent Name: '.$continent.'
Continent Code: '.$continentCode.'
In the EU?: '.$inEU.'
EU VAT Rate: '.$euVATrate.'
Radius of Accuracy (Miles): '.$locationAccuracyRadius.'
Currency Symbol: '.$currencySymbol.'
Exchange Rate: '.$currency_exchange.'
Not this only you can get all the available data found in the array of the API result here.
Now lets see some important info about this API service provider.
Geoplugin is one of the most popular and best Location information api provider out there.
How many Geoplugin API call limit?
Geoplugin API call limit is 120 call pre minute, Greater than 120 API requests per minute will get you blacklisted for 1 hour, during that time you will receive a "429 Too Many Requests" response.
If you want to have more than 120 API call per minute then simply you should upgrade to their premium plan, you can see their premium plan here
My Advice in using this API from my own experience is
- Take control of your user's http request,
If the request is comming form bots, spiders or crawlers keep this API look up away using else if statement, so you can track a real users only, By doing this You can't exceed the on minute API call limit.
- Put your neccessary data in SESSION
Do you know putting your neccessary data in session and use data session once its setted up so no need to call to that API again when your visiotr navigates to different page in your domain. So by doing that you will have minimum look up, and less server response delay time. I mean your Server doesn't have to wait for the API reponse in each page request. It just get the data from already stored in SESSION. Just call API when a new visitor arrives to your Website only, then store all the neccessary data returned from the API in SESSION so no need to call API again when user navigates to different page of your site.
Now that's it from me.
If you're statsfied with this article you will probably share or comment below. If not or have a question Contact me directly.
Thank you 😊
Report for Comment or Reply
You May Like These Posts
How to Build your own BMI calculator using PHP, AJAX, a...
How to Build your own BMI calculator using PHP, AJAX, and jQuery without refresh the page
What is User Agent parser API? - Userparser
Parse your users user-agent and get your users Browser & OS names and versions, Device name, brand, type, viewport width & height etc...
What data a website can collect from you? - Userparser
What are the data that any website can collect with or without your consent when you visit it?