PHP Get User Country Location From IP Address (Step-By-Step Guide)

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”, but each region actually has its own specified range of addresses. We can do a reverse check and determine where a user is from with their IP address. This is called “IP geolocation”.

The steps to get the user’s location in PHP are:

  1. Sign up with an online IP geolocation service such as ipgeolocation.io
  2. Get the API key and endpoint URL from the dashboard.
  3. 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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TUTORIAL VIDEO

 

TABLE OF CONTENTS

Download & Notes IP Geolocate Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

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.

 

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

geo-ip.php
<?php
// (A) SETTINGS
$settings = [
  "apiKey" => "YOUR-API-KEY",
  "ip" => $_SERVER['REMOTE_ADDR'],
  "lang" => "en",
  "fields" => "*"
];
 
// (B) INIT CURL + OPTIONS
$ch = curl_init();
$url = "https://api.ipgeolocation.io/ipgeo?";
foreach ($settings as $k=>$v) { $url .= urlencode($k)."=".urlencode($v)."&"; }
$url = substr($url, 0, -1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// (C) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) {
  // (C1) CURL FETCH ERROR
  echo curl_error($ch);
} else {
  // (C2) CURL FETCH OK
  $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, 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.

 

 

USEFUL BITS & LINKS

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.

 

INFOGRAPHIC CHEAT SHEET

PHP Get User’s Location From IP Address (Click To Enlarge)

 

LINKS & REFERENCES

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *