0

I am trying to redirect the website according to their country region. how do I get the country code without using the geoip. Can any one help me?

saikumar
  • 43
  • 2

2 Answers2

0

I would suppose you have to ask some party to map the IP address into a locatio, thus check the other questions such as Identify country by IP addresss.

I would suppose that services doing this, are internally relying on multiple different services which are holding the regional data, thus it is really the easiest solution to use them, instead of trying to go finding the country in your own service.

Community
  • 1
  • 1
Dr.Jukka
  • 2,346
  • 2
  • 15
  • 20
0

Method 1

You can use geoIP for apache to redirect. The same has been answered here as well.

Method 2

Many times in shared hostings, you can not install additional modules. In those cases you can use a third party api service, like http://www.geoplugin.net/.

Here's a function to use get Country details:

function getLocationInfoByIp(){
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = @$_SERVER['REMOTE_ADDR'];
    $result  = array('country'=>'', 'city'=>'');
    if(filter_var($client, FILTER_VALIDATE_IP)){
        $ip = $client;
    }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
        $ip = $forward;
    }else{
        $ip = $remote;
    }
    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    
    if($ip_data && $ip_data->geoplugin_countryName != null){
        $result['country'] = $ip_data->geoplugin_countryCode;
        $result['city'] = $ip_data->geoplugin_city;
    }
    return $result;
}

Ref.

You can use the above function to redirect users:

$visitor = getLocationInfoByIp();
if($visitor["country"]=="HK"){
//Redirect to Hong Kong Site.
}
elseif($visitor["country"]=="ES"){
//Redirect to Hong Kong Site.
}

Method 3

You can use Hostip APIs. More here.

http://api.hostip.info/country.php
  US

http://api.hostip.info/get_html.php?ip=12.215.42.19
  Country: UNITED STATES (US)
  City: Sugar Grove, IL
    IP: 12.215.42.19

http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true
  Country: UNITED STATES (US)
  City: Sugar Grove, IL
  Latitude: 41.7696
  Longitude: -88.4588
    IP: 12.215.42.19

http://api.hostip.info/get_json.php
  {"country_name":"UNITED STATES","country_code":"US","city":"Sugar Grove, IL","ip":"12.215.42.19"}

http://api.hostip.info/?ip=12.215.42.19
  [use the URL above for an example - XML too long to paste below]
Community
  • 1
  • 1
Santosh Achari
  • 2,936
  • 7
  • 30
  • 52