Changing the home page based on visitor's country information
For my web site www.books4brains.com I wanted to change the order in which my books are displayed based on the visitor's country. We have titles in Dutch and I want visitors from The Netherlands to see the Dutch titles first. For German visitors I want to display German books first and for all other visitors I list the English books first.
Note: I explicitly state here that I want to change the order. If I can't detect the country I default to displaying the English titles first. I don't want visitors to see a blank page if they visit the site from, let's say: Bermuda (BM).
The page consists of two components
- A routine to find the visitor's location information based on the IP address. (I found that on the web and have used it as is. No explanation here, it's better to search for this on the Internet if you need help
- A PHP switch construction that checks the country code and displays the information in the correct order.
In the PHP Switch-construction you find below it is important to note the break command within each case-statement. You want to stop processing when a valid country-code was found. Also I have added a default section which displays the content when no predefined country code (here NL and DE) were found.
In the case-statements in the page below I have not displayed the actual content here to keep the sample readable. Also I don't want to copy the same HTML-code into each case statement. So what I did is use another tip listed here to include other pages containing the book information per language and embed them with a PHP statement in each case-statement in a different order.
The complete page is listed here:
<?php
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function countryCityFromIP($ipAddr)
{
//function to <strong class="highlight">find</strong> <strong class="highlight">country</strong> and city from IP address
//Developed by Roshan Bhattarai [url]http://roshanbh.com.np[/url]
//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array
//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
//assing the city name to the array
$ipDetail['city']=$match[2];
//get the <strong class="highlight">country</strong> name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
//assign the <strong class="highlight">country</strong> name to the $ipDetail array
$ipDetail['country']=$matches[1];
//get the <strong class="highlight">country</strong> name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the <strong class="highlight">country</strong> code to array
//return the array containing city, <strong class="highlight">country</strong> and <strong class="highlight">country</strong> code
return $ipDetail;
}
$realip = getRealIpAddr();
$locationdata = countryCityFromIP($realip);
?>
<?php switch($locationdata['country_code']) {
case "NL": ?>
Show Dutch content here, followed by Englsih and German
<?php break; case "DE": ?>
Show German content here followed by English and Dutch
<?php break; default: ?>
Show English content here followed by German and Dutch
<?php } ?>
For comments and questions feel free to contact me





