Welcome to a quick tutorial on how to get the user’s country and location from their IP address. Yes, IP addresses are not “randomly distributed”, and each region has its own specific range of addresses. We can do a reverse check and determine where a user is from with their IP address. This process is called “IP geolocation”.
The steps to get the user’s location in PHP are:
- Sign up with an online IP geolocation service such as ipgeolocation.io.
- Get the API key and endpoint URL from the dashboard.
- In your PHP script, make a CURL call to the endpoint URL. Send the user’s IP address along with the API key, the geolocation service will return the user’s location data.
That’s all. No need to panic at the words “API” and “CURL”, it is not as difficult as some think – Read on for a simple example!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
IP GEOLOCATE WITH PHP
All right, let us now get into an example of how to get the user’s country with their IP address.
TUTORIAL VIDEO
STEP 1) SIGN UP WITH GEOLOCATION SERVICE
First, Captain Obvious to the rescue. Head over to ipgeolocation.io and sign up for a free account.
STEP 2) GET THE API KEY
Next, grab your API key from the dashboard. Take note that at the time of writing, free accounts are limited to 1000 requests a day. If you need more, you will need to sign up for a paid key.
STEP 3) CURL CALL TO THE API ENDPOINT
<?php
// (A) SETTINGS + URL
$settings = [
"apiKey" => "YOUR-API-KEY",
"ip" => $_SERVER["REMOTE_ADDR"],
"lang" => "en",
"fields" => "*"
];
$url = "https://api.ipgeolocation.io/ipgeo?";
foreach ($settings as $k=>$v) { $url .= urlencode($k)."=".urlencode($v)."&"; }
$url = substr($url, 0, -1);
// (B) INIT CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// (C) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) { echo curl_error($ch); }
else {
$info = curl_getinfo($ch);
$result = json_decode($result, 1);
print_r($result); // geo ip information
print_r($info); // more information on the curl call
}
curl_close($ch);
Do a CURL call to the API endpoint, and send the user’s IP address along with your API key. Lastly, JSON decode the results, and that’s it. The end.
P.S. There are plenty of random IP generators online that you can use for testing.
EXTRAS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
IP-GEOLOCATION IS NOT 100% ACCURATE!
That’s right. Before you get too happy, here’s a quick notice:
- IP-Geolocation can only get the general region of where the user is at.
- Users can be on a VPN or proxy server. That is, bouncing their original connection off another server.
So if you want an accurate reading, you should be looking into GPS instead. Link below.
LINKS & REFERENCES
- Top 10 Best IP Geolocation APIs – rapidapi.com
- How To Get GPS Coordinates & Generate Map In Javascript – Code Boxx
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
You can sign for free IP2Location.io API which is faster.
Thanks for the nice info. It’s really very easy and beneficial for e-commerce businesses.