Serving different content to your visitors by country
Here at Cloud Testing, we decided to start serving different content to different visitors depending on where in the world they come from.
“Why would you want to do that ?” you might ask, well we use it to offer special offers to customers in markets or countries we’re targeting. You might also not be interested or able to provide your service in a country.
It’s all done server side based on the IP that they come from, and makes use of the service from IP Info DB – http://www.ipinfodb.com/
In PHP you can get the country of the visitor using the following code (for PHP5):
function visitorCountry() {
$ip = getenv('REMOTE_ADDR');
$d = file_get_contents(
"http://www.ipinfodb.com/ip_query_country.php?ip=$ip&output=xml");
//Use backup server if cannot make a connection
if (!$d) {
$backup = file_get_contents(
"http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml");
$answer = new SimpleXMLElement($backup);
if (!$backup)
return false; // Failed to open connection
} else {
$answer = new SimpleXMLElement($d);
}
return $answer->CountryCode;
}
Once you have this, you can re-direct visitors to specific pages (in this case US customers).
$sCountryFrom = visitorCountry();
if ( $sCountryFrom == 'US' )
{
header('Location: /special_offer_us.php');
exit(0);
}
or customize content on a page, change prices dynamically, serve language specific content (in the following example using Smarty).
if ( $sCountryFrom == 'DE' ) { // serve German content template if they are from Germany$cSmarty->display('/index_de.tpl'); } else { // serve English to everyone else $cSmarty->display('/index.tpl'); }
Did you enjoy this post? Why not subscribe to our feed and get articles like this delivered automatically to your feed reader.

Comments
No comments yet.
Sorry, the comment form is closed at this time.