jul
2008
Recording users by country
Detecting a user’s location requires recording and analysing their IP address. Different IP ranges are assigned to different countries. This isn’t always full proof, for example if the ISP is in another country from the user it will be recorded incorrectly. After taking note of the IP the easiest way to detect the country is to use a predefined database such as ip2nation (http://www.ip2nation.com/ ). Ip2nation is a free mySQL database of IP’s and countries. However it is isn’t documented, so takes a moment to work out how you want to use it. You can download it here: http://www.ip2nation.com/ip2nation/Download
SELECT c.country, c.code
FROM ip2nationCountries c, ip2nation i
WHERE i.ip < INET_ATON('77.99.108.82')
AND c.code = i.country
ORDER BY i.ip DESC
LIMIT 0,1
This basically selects the country and country code for the given IP address.
To store the information in the database the simplest way to do it is have a table with ip, countrycode, timestamp. Where IP is a long value and primary key, countrycode is a string foreign key from the table ip2nationCountries. Finally timestamp is a timestamp that automatically assigns the current timestamp. Here is a phpMyAdmin dump of the table I use to store the data...
CREATE TABLE user_country (
ip bigint(20) NOT NULL default '0',
countrycode varchar(5) NOT NULL default '',
time timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (ip)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
The ip address is stored as a primary key to enforce it as unique. However, this is optional if you want to record users more than once. I am going for absolute unique visitors only. So if your IP is static, you’ll only ever be recorded once. The main downside of this is it often counts large institutions as one, universities for example may only have a limited number of IP’s.
Finally here is a quick example for recording that IP address with the country. Note you will need to change the IP address in both places to make it work.
INSERT INTO user_country (ip, countrycode)
VALUES (INET_ATON('77.99.108.82'), (
SELECT c.code
FROM ip2nationCountries c, ip2nation i
WHERE i.ip < INET_ATON('77.99.108.82')
AND c.code = i.country
ORDER BY i.ip DESC
LIMIT 0,1
))
Now all you need to do is wrap this up in your favourite language and start recording your users’ countries. It’s quite interesting to see where people are coming from. You can see a list of the visitors to my website on the right Colum of this blog just below the recent posts.
There are however, some limitations. This script doesn’t take into account bots and spiders. I don’t know how many of the logged IP’s are not from regular people. Also, if your ISP happens to be in a different country, then you will get the incorrect IP. Finally, if you use mini opera on your mobile phone, you will appear as Norwegian as all mini opera traffic passes through Norway to compress the content. That’s why it loads so fast!
Short url - Related tags: country-detection, ip, ip2nation, mysql, sql, stats