I've recently been looking into options of adding relevant advertisements to my site and ran a test of some Google AdSense ads, which supports displaying ads relevant to the visitor's country.
Unfortunately, AdSense did not do a terribly great job of optimizing the displayed ads for my site and so I've decided to take matters into my own hands and start adding Amazon ads to certain sections. By doing this I'm able to pre-select a range of products to be displayed when viewing particular pages of my websites (some code and a back end database went into this but it was a few hours work).
Unfortunately however, each of the international arms of Amazon has their own affiliate scheme, and so you can't use the same code to target US and UK visitors. Instead, you have to sign up with each one of these affiliate schemes and display the relevant ad code depending on the country the visitor is from (if you want to maximise clicks and potential sales).
To do this on a website, the usual method is to take the visiting IP address and track it back to the country of origin. In the past I have used
MaxMind's GeoIP for this, but I Googled 'IP to Country' and the top link took me to a project on Google Code named
ip-countryside. I clicked through and had a look and I like the streamlined method it uses to have decided to run with it. ip-countryside uses a database of IP ranges and the country associated with them from their RIR records (the master datbases which show you who owns certain IP addresses).
I downloaded ip-countryside and was pleased to see a demo PHP file in the distribution but was a little dismayed at the method by which it was performing lookups. The database comes as a 2.9MB text file and the PHP script literally reads the entire file into memory and then scan it to find a range into which the requested IP fits. This is highly inefficient and I decided I would import the flat file database into a MySQL database and write my own little lookup script:
CREATE TABLE `db`.`ip-countryside` (
`start` INT UNSIGNED NOT NULL,
`end` INT UNSIGNED NOT NULL,
`country` VARCHAR( 2 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY ( `start` ),
INDEX ( `country` ),
UNIQUE ( `end` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
LOAD DATA LOCAL INFILE '/tmp/ip2country.db'
INTO TABLE `db`.`ip-countryside`
FIELDS TERMINATED BY ' '
LINES TERMINATED BY '\n';
I then wrote a very small PHP script which I can use in my site to detect the IP of the visitor and use the returned country code to help show the correct ads.
Using a MySQL database rather than a flat file sped up the lookups from an average of 0.5 seconds to 0.025 seconds per lookup. That's 20 times faster!
Here's the PHP file I use to do the lookup. Note that I store the identified country in a session variable, and on successive loads I just used the stored variable rather than repeatedly hitting the database:
I've also implemented a basic service using a slightly modified script which others can use (though I will monitor it and if it gets abused I reserve the right to remove the service). To perform a country lookup for a particular IP you simply have to pass the IP to the following URL and record the returned 2 letter country code:
http://martincaine.com/tools/ip-countryside/?ip=94.102.149.34
If for whatever reason the script can not identify the country from the given IP address the script will default to return the 2 letter code
US. You can optionally pass a second argument to the script to specify the default which will be retuned if the IP address can not be found:
http://martincaine.com/tools/ip-countryside/?ip=244.102.149.34&default=DEFAULTVALUE
I hope this is of use to someone, I'm aware there are many such services on the internet but this was something I needed to implement for my own site anyway so thought I'd share it with the rest of the world :).
If you found this post helpful please leave a comment below: