How geotarget location works in PHP

geotarget using php

PHP Geotarget Location Code

In this tutorial we are going to learn how Geotarget location works and then we will code a small script to find country and then show flag of that country.

So first of all we need to understand how to find IP address of the visitor ? here in this tutorial we can read : How to get Real IP from Visitor?

Once we are table to find IP address of the visitor. We have to find out his country location. As we know every IP address is assigned to some services or net providers or data centers and those services / net providers or data centers are established in countries. So with the help of IP address we will get the country name. Lets check how to find country from IP Address.

#IP HUB PROXY CHECK#  
function getIpDetails($ip)
{
    global $iphubkey;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_URL, 'http://v2.api.iphub.info/ip/'.$ip);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Key:'.$iphubkey));
      $result = curl_exec($ch);
      curl_close($ch);
      $obj = json_decode($result, true);
        #IP DETAILS FOR VISITOR#
        $ip = (empty($obj['ip'])) ? '' : $obj['ip'];
        $countryname = (empty($obj['countryName'])) ? '' : $obj['countryName'];
        return $ip.'#'.$countryname;
}

By using iphub.info API service ( iphub.info is giving 50 request / min for free API services ) for small websites this is good. Above code we made which lets discuss that here.

As you read in preview tutorial : How to get Real IP from Visitor?  ( we got IP address of visitor using $_SERVER[‘REMOTE_ADDR’])

Then after we passed that IP into above code function. This code will return us IP # CountryName for eg: 199.154.70.3#United States

So we are able to get Country Name from the code above. Once we get that next step is to find Country flag from the folder we installed for the flags.

Summarizing the code :

$iphubkey = 'YOURIPHUBKEY';

#IP HUB PROXY CHECK# 
function getIpDetails($ip) { 
global $iphubkey; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, 'http://v2.api.iphub.info/ip/'.$ip); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Key:'.$iphubkey)); 
$result = curl_exec($ch); 
curl_close($ch); 
$obj = json_decode($result, true); 
#IP DETAILS FOR VISITOR# 
$ip = (empty($obj['ip'])) ? '' : $obj['ip']; 
$countryname = (empty($obj['countryName'])) ? '' : $obj['countryName']; 
$proxy = (empty($obj['block']))? '':$obj['block']; 
return $ip.'#'.$countryname;
} 


$ip = '199.154.70.3'; 

$visitor = getIpDetails($ip); // Passing the IP address into function to get results.
$visitor = explode('#',$visitor); // this will explode the visitor result from # so IP and countryname will become separate


$ipaddress = $visitor[0];
$countryname = $visitor[1];

echo '<img src="flagfolder/'.$countryname.'.jpg" />';

 

I hope this tutorial helps our reader to understand how to make applications geotarget.

Leave a Reply

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

30  −  22  =