Get visitor country, city, currency exchange & many more using PHP
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 analytic 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 for free forever.
You can do this using your preferred 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...
$your_visitor_ip_address = $_SERVER['REMOTE_ADDR'];
Now All starts using Geoplugin URL with your user's ip address at the end.
Now to get the returned content you need to insert the above URL inside file_get_contents function looks like
$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, what's 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
$get_all_data_list = unserialize($get_data);
Now let's get all the visitor location info
$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
$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.': City: '.$city.' Region: '.$region.' 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.' Latitude: '.$latitude.' Longitude: '.$longitude.' Radius of Accuracy (Miles): '.$locationAccuracyRadius.' Timezone: '.$timezone.' Currency: '.$currencyCode.' 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 calls are allowed?
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,
To avoid exceeding the one minute API call limit, you can exclude bots, spiders, or crawlers from API lookups by using an else-if statement. This way, you can track only real users and keep API calls limited to necessary requests.
- Put your necessary data in SESSION,
Did you know that you can optimize the performance of your website by storing necessary data in sessions and then using this data throughout the session? This way, you won't have to call the API repeatedly when your visitor navigates to different pages within your domain. By doing this, you can minimize lookups and reduce server response delay times. Your server won't have to wait for the API response for each page request as it can retrieve the data from the already stored session.
You only need to call the API when a new visitor arrives on your website. Once you receive the necessary data from the API, you can store it in the session so that you don't have to call the API again when the user navigates to different pages on your site. This can significantly improve the performance of your website and enhance the user experience.
Now that's it from me.
If you're satisfied with this article you will probably share or comment below. If not or have a question contact me directly.
Thank you 😊
Full stack Web developer, Founder of Mefth and Userparser.
Report for Comment or Reply
Get Notified through your email for new Blogs.
You may like these posts
How to send emails using a new method of...
Google does not allow Less secure apps feature anymore, So here is the easiest s...
How to Build your own BMI calculator usi...
Build your own BMI calculator using PHP, AJAX, and jQuery without refreshing the...
4 easiest methods of creating a NEW webs...
Website is the place you can sell or buy things, share knowledge or doing other ...
How to disable right click and F12 Key u...
HTML Meta tags you have never seen befor...
Here are the most useful and you have never heard of HTML meta tags