Geolocation is the identification of the real-world geographic location of an object, such as a radar, mobile phone or an Internet-connected computer terminal.

An IP address is Internet Protocol address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. Read more about IP address in wiki.

While working on websites, there are times when I would like to display/hide or add/remove certain elements in web page depending on the location of user. Not only that, one can take many advantages by knowing the user’s location like geo targeted advertisements, suggested news, e.t.c.

I did use it today with an API, calling it via PHP Curl. Below is example code.


<?php
 function isFromNepal() {
     $ch = curl_init(); //initialize curl
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_URL, 'http://freegeoip.net/json/' . $_SERVER['REMOTE_ADDR']); //URL to send the IP
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $geoData = json_decode(curl_exec($ch)); //Decoding the json data returned.
     curl_close($ch); //close curl
      
     if ($geoData->country_name == 'Nepal') {
        return true;
     } else {
        return false;
     }
 }

This function will return boolean(true/false) and you can execute other codes as required. Above function can be customized as needed to return full data about location rather than just checking a parameter and returning true or false. Details given in this site can help you more for easy customization.

Hope it was helpful.