-1

I'm making a mobile website and would like to load a particular script if a user visit my site from a particular country

For example:

User from country A --> load A script

User from other countries --> load B-script

After searching on the internet, the simplest way is to use Hostip service

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

So it returns what is user's current location.

But how can I extract the return value, and use it in a IF statement?

JUST LIKE THIS:

if (return value of http://api.hostip.info/country.php = US){
load script for US user;
} else {
load script for users from other countries
};

Many thanks :)

4 Answers 4

2

Doing file_get_contents('http://api.hostip.info/country.php)) will surely return the country of the webserver, not the country of the visitor?

We used to use IP2Nation but have found it to be inaccurate lately so we switched to this:

http://dev.maxmind.com/geoip/legacy/geolite/

In our instance we downloaded the country data in CSV format: http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip

We then put this data into a mysql table like this

CREATE TABLE `geoip` (`ipstart` int(10) unsigned NOT NULL, `ipend` int(10) unsigned NOT NULL, `code` char(2) NOT NULL, PRIMARY KEY  (`ipstart`,`ipend`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `geoip` (`ipstart`, `ipend`, `code`) VALUES (16777216, 16777471, 'au'),
(16777472, 16778239, 'cn'),
(16778240, 16779263, 'au'), 
etc...

$_SERVER["REMOTE_ADDR"] doesn't always return the correct IP address, if someone is behind a proxy for example. You can determine a visitor's real IP address (as best as possible) with this function

function get_real_ip () {
  if (!empty ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    return $_SERVER['HTTP_X_FORWARDED_FOR'];
  } elseif (!empty ($_SERVER['HTTP_CLIENT_IP'])) {
    return $_SERVER['HTTP_CLIENT_IP'];
  }
  return !empty ($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
}

Then you can get a customer's country using a PHP function like this:

function get_country_by_ip ($ip) {
  $ip = ip2long ($ip);
  $sql = 'SELECT code FROM geoip WHERE ipstart <= ' . $ip . ' AND ipend >= ' . $ip . ' LIMIT 1';
  // Get result of mysql query and return it here
}

So your final code would be:

define ('COUNTRY_CODE_US', 'us');
if (get_country_by_ip (get_real_ip () == COUNTRY_CODE_US) {
  // load script for US user;
} else {
  // load script for users from other countries
};

This solution has the advantage that you have the ability to do different things for users in different countries, not just the US, and you aren't relying on calls to third-party websites/API's which might fail

2
  • Thank you for your detailed reply. I think this answer should work. However, as I'll not be able to modify mysql (due to some restriction), so I would like to try to use file_get_contents("http://api.hostip.info/country.php?ip=".$_SERVER["REMOTE_ADDR"]); together with the get_real_ip function you suggested. Do you think it will also work?
    – Kit Ng
    Aug 29, 2013 at 18:00
  • Yes that should be fine. Since you're calling an external resource you might want to check that the result of file_get_contents() isn't false (using ===). If it returns false you could provide an additional script based on the fact you weren't able to find out whether the visitor was in the US or not.
    – MrCarrot
    Aug 29, 2013 at 20:05
0

Give this a go.

$country = file_get_contents('http://api.hostip.info/country.php');

if($country == 'US') {
  echo "You're in the US";
} else if ($country == 'Mexico') {
  echo "Yeah, you're in Mexico.";
}
0

To identify county users you must know they IP and then send ip to api
example

<?php
$ip= $_SERVER["REMOTE_ADDR"]; //gives ip
$json=file_get_contents("http://api.hostip.info/get_json.php?ip=".$ip); // return json ,
//example {"country_name":"UNITED STATES","country_code":"US","city":"Aurora, TX","ip":"12.215.42.19"}
// you need remember country_code

$arrayCounty=json_decode($json,TRUE); //json in array

//and then your if
if ($arrayCounty["country_code"]=="US"){
    echo("people from US");
}
else{

}

?>
0

You can use file_get_contents() for this:

if ('US' == $loc = file_get_contents('http://api.hostip.info/country.php')) {
    echo 'From US...';
} else {
    echo 'From ', $loc;
}

Just as a basic start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.