Obtaining and Using the Visitor's Country
Sometimes it may be desirable to customize the content of a webpage based on where a visitor comes from (e.g., display the appropriate country flag, or decide which currency to show). On this website it is used in order to show different advertisements for different countries; British visitors should see amazon.co.uk banners, French visitors should see amazon.fr banners, etc. Silverstripe does have a module called "geoip" that already provides this service, however, it relies on certain executable files being present on the server (and permission to execute them) which this website's server does not have. Thus, this project was born.
Obtaining the Visitor's Country
The visitor's country is determined by comparing his/her IP address to a database. The basic steps in order to add country to a website detection are:
- Download and install the IP-to-country database from http://ip-to-country.webhosting.info,
- Download and install the PHP script that contains the country lookup code (i.e., ip-to-country.php), and
- Call the PHP script's getVisitorCountry() function from the website's CMS (e.g., Page.php).
Obtaining and Installing the Country Database
The first step is to obtain the database, which is provided by WebHosting.Info (http://www.webhosting.info), and is available from http://ip-to-country.webhosting.info. The database is provided as a CSV file. You will need to create a new MySQL database, containing a table, preferably called "countryDB", and create a user-name and password in order to access the database. Details on the format of this table can be found in the ip-to-country handbook, here. Next, import the CSV data into the database. This can be achieved via phpMyAdmin, or other means. Please note that the database may be too large for phpMyAdmin to import in one go. I personally opened the CSV file in MS Excel, and split it into two approximately equal files. If you split the file using MS Excel, be very careful with the save settings, as you want to preserve the format. Alternatively, you could use a text editor to split the file into sections. I have also been told that Navicat (an alternative to MyPHPAdmin) can handle the entire file in one go.
Installing the IP-to-country PHP Script
- $countryDBServer = "localhost";
- $countryDBUserName = "";
- $countryDBPassword = "";
- $countryDBName = "countryDB";
Silverstripe users will probably want to put this in a sub-directory of the "mysite/code/" directory.
Using the Script
The script has a single function called getVisitorCountry(), which returns an array containing the following elements:
- 'CountryCode', e.g., "NZ", and
- 'CountryName', e.g., "NEW ZEALAND".
Details on using the script in Silverstripe are provided below.
Adding Visitor Country Code to SilverStripe's Page Class
For Silverstripe users, an example Page.php file is provided which basically contains the following additions to the basic Silverstripe Page.php code:
At the top of the script:
At the top of the Page_Controller class:
var $visitorCountryCache = FALSE;
The above class field is used to cache the result so that a template can check the visitor's country multiple times without having to perform a database lookup each time. Finally, the VisitorCountry() method:
if($this->visitorCountryCache == FALSE)
$this->visitorCountryCache = new ArrayData(getVisitorCountry());
Customizing Content Based on the Visitor's Country (in Silverstripe)
With the code above put into Silverstripe's Page_Controller class, it is now possible to obtain and use a visitor's country in a template, for example, if one wished to display the name of the country, and show a flag, the following code could be used:
You're country is: $VisitorCountry.ContryName
The code above assumes that a directory called "flags" is present that contains flags as GIF images, with their names corresponding to the two-letter country codes, (e.g., "NZ.gif").
Sometimes it may be desirable to display blocks of content tailored for a particular country (e.g., special deals available only to visitors from the website's home country). If there are not too many countries in the list that require customization, the following code would work:
<% control VisitorCountry %>
<% if CountryCode == NZ %>
-- Special for New Zealanders --
<% else_if CountryCode == AU %>
-- Specials for Australians --
<% else %>
-- Content for everyone else --
<% end_if %>
<% end_control %>
If the list of countries requiring customized content is large, it may be worth creating a Silverstripe module that stores these blocks of content into a database, rather than cluttering up your templates. This should also be faster.
WARNING: Use customization based on country sparingly. It can render content inaccessible to search engines, and/or confuse visitors who have friends in other countries.
The files belonging to this project are:
Projects » Silverstripe/PHP Projects » Obtaining and Using the Visitor's Country