More Info »
How to get Country from IP address
To better target our products, we need to know who are our visitors and from where they are coming from. From referral string (search engines) it can be possible to locate country (uk.search.yahoo.com google.co.uk for example). In an other case, when visitor is not using local search engines, or he is coming from some other website, resolved IP address can show country, sometimes. For all other cases, we can find Country from IP address using this calculation below.
IP address to IP number calculation
IP address is in this format: 111.222.333.444, and it is located in the first field of you raw website log.
To make this more simple, we will use A.B.C.D instead of 111.222.333.444. Formula to translate Ip address to IP number is below:
A x (256*256*256) + B x (256*256) + C x 256 + D
IP number to Country
With IP number alone, we can't know too much. Now we need an ip2country database to find country from IP number. There are few of them, even IP2City is available, some are free, some paid. For this purpose, I am using IP2country database located at software77.net (new window), it is free, .csv format and updated often. There are also some scripts for download, what can be used too.
First two row fields in that database are low IP number and high IP number. Now, we need to find database row where our number is between those two, and pull Country (short or long) from that row.
There are many online services what are doing the same, translating IP to country, or simple IP to DNS, above example is example how to do it locally, when you need to translate more IPs to country, without abusing those services.
On Get search engine position from website log is explained how to store search engine referrals from one page to data file. Pulling IPs from that file and converting them to Country, from resulting report you can know what kind of product you need to push on that page (according to Geo data - countries).
ip2country database can be used, along with script, like some kind of online service for your visitors, with all those small nice country flags.
Like you see, converting IP to Country is not that hard, with little programming, or using some script available for free.