Easy to use PHP class to detect client location

By Faruque Ahamed Mollick

Here in this post, we are going to see a PHP class which can be used to detect client location. The class is so easy to use. It will return the city name, country name, country code, latitude and longitude of the client.

In our code, we have used IPInfoDb API to detect the location from the IP address. Below is the code of the PHP class:

<?php

/**
* This is a PHP class that will detect the client location using IpInfoDb API
*/
class ClientLocation
{
  private $appkey, $userip;
  function __construct($key)
  {
    $this->appkey = $key;
    $this->userip = $this->get_client_ip();
    if (strlen($this->userip) < 6) {
       echo "<span style='color:red;border:solid 2px red;padding:4px;font-size:1.1em;'>IP address cannot be detected offline or on localhost server or some other issue may occur.</span>";
       exit();
    }
  }

   private function get_client_ip() {
        $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
           $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
   }

  private function iiddecode() {
    $jsoncont = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=".$this->appkey."&ip=".$this->userip."&format=json");
    $getdecoded = json_decode($jsoncont, true);
    return $getdecoded;
  }
  public function city() {
       return $this->iiddecode()['cityName'];
  }
  //country
  public function country() {
       return $this->iiddecode()['countryName'];
  }
  // country code
  public function countrycode() {
       return $this->iiddecode()['countryCode'];
  }
  // regiom name
  public function region() {
       return $this->iiddecode()['regionName'];
  }
  // let from IP
     public function latFromIp() {
       return $this->iiddecode()['latitude'];
  }
  // lon from IP
    public function longFromIp() {
       return $this->iiddecode()['longitude'];
  }
}

?>

 

How to use the above PHP class to detect client location?

Using the above class you just need to write a line of code to create an object of the class and simply echo it to display on web page. For example, suppose you have created an object $obj. Now to show the city name, just use the code echo $obj->city();

Let’s create a new file ClientLocation.php and after that create another PHP file which will include ClientLocation.php in that file just like below:

<?php
   // Include the file contains the ClientLocation class
   include 'ClientLocation.php';

   // Creating the object of ClientLocation class 
   //Put your IPINFODB API KEY
   $obj = new IpInfoDb('IPINFODB_API_KEY');

   // Display the city name
   echo $obj->city();
?>

 

The above code will display the city name. Similarly, we can show the country name, country code, region, latitude and longitude. Below is the list was given:

City name: city()

Country name: country()

Country code: countrycode()

Region: region()

Lattitude: latFromIp()

Longitude: longFromIp()

Here is another example to show the longitude and latitude:

<?php
   // Include the file contains the ClientLocation class
   include 'ClientLocation.php';

   //Put your IPINFODB API KEY
   $client_location = new IpInfoDb('IPINFODB_API_KEY');

   // Display the lattitude
   echo $client_location->latFromIp();

   // Display the longitude
   echo $client_location->longFromIp();
?>

 

So friends, Did you like this post? I hope you find this post interesting. Please comment below if you have to say anything related to this tutorial.


Ajaxify Weather Forecast PHP Script Source Code

YouTube Video Downloader PHP Script – Download Source Code

 

Tags:

Leave a Reply

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