Get user location from IP address with php

Get User Location From IP address with php




In this post we are going to learn how to Get User location from IP address with php. Getting user location from IP address is quite easy, For this tutorial we will be using telize,Telize Provide results in json format that we can easily manipulate in php. So Lets Get Started



Get user location from IP address with php

Step 1

Head over to telize and find a GeoIP Or you can simply type http://www.telize.com/geoip/46.19.37.108
Get user location from IP address with php

Step 2

Type GeoIP or http://www.telize.com/geoip/46.19.37.108 into the browser and you will see something like this
Get user location from IP address with php
The url is basically displaying all the results in the json format (Don’t worry if you didn’t see result same as the image, I am using Google Chrome Plugin to display json in pretty format) we can get the required information easily

File Setup

Geo.php

We are going to create a class by which we will be able to fetch desired information from the IP address

  1. First up we will be creating 2 properties(variable) $api & $properties, In $api we will put GeoIP string but we will be replacing The IP Address 46.19.37.108 with %s
  2. Create a empty array of $properties, we will be putting the fetched result inside this array
  3. Create a method(function) request, By This Method we will be replacing %s From $api with real IP address by using sprintf(), next we will be calling the getRequest() method to fetch the data from the server and we will be storing the data into $properties in the array format (Remember the result is json format) to convert json into array we will be using json_decode()
  4. Next Up getRequest() method, In this method we will be using CURL library to fetch the results from remote database What is CURL?
  5. We will be using __get() magic method. to call a index of the $properties array without declaring them. What is __get() magic method?
<?php
class Geo
{
    protected $api = "http://www.telize.com/geoip/%s";

    protected $properties = array();

    public function __get($key){
        if(isset($this->properties[$key])){
            return $this->properties[$key];
        }else{
            return null;
        }
    }

    public function request($ip){
       $url = sprintf($this->api, $ip);
        $data = $this->getRequest($url);

        //set this $data which we get from telize api into $this->properties and make it an array
        $this->properties = json_decode($data, true);

    }

    protected function getRequest($url){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl,CURLOPT_URL,$url);
        return curl_exec($curl);
    }

}

index.php

  1. In Index.php page we will be including Geo.php class
  2. we will be instantiating the class Geo.php class
  3. Call the $geo->Request(IP ADDRESS)
  4. Displaying the desired result $geo->city
<?php
    require_once("Geo.php");

    $geo = new Geo();

    //get the IP address of the user
    $ip = $_SERVER&#91;"REMOTE_ADDR"&#93;;
    
    $geo->request("123.238.178.69");

   	echo "Look Like you Live in ".$geo->city;
?>

Leave a Reply

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