{"id":43,"date":"2006-09-13T21:38:00","date_gmt":"2006-09-14T04:38:00","guid":{"rendered":"http:\/\/www.1keydata.com\/wordpress\/?p=43"},"modified":"2007-09-21T17:42:52","modified_gmt":"2007-09-22T00:42:52","slug":"convert-ip-address-to-country","status":"publish","type":"post","link":"https:\/\/www.1keydata.com\/blog\/convert-ip-address-to-country.html","title":{"rendered":"Convert IP Address to Country"},"content":{"rendered":"<p>One exercise that you&#8217;d like to do is find out which countries your visitors are coming from. There are several reasons for doing this:<\/p>\n<p>1) You might want to tailor your content to that particular demographic.<\/p>\n<p>2) If you have lots of visitors from a particular country, you might want to consider adding a version of your website in that particular language.<\/p>\n<p>3) If your site has IP-based targeting for ads (programs such as Adsense have an IP-targeting component), this will help you understand why, or why not, your users are clicking on your ads.<\/p>\n<p>The way I did it was to download a flat file that include IP-to-country mapping data from the link found <a target=\"ip\" href=\"http:\/\/ip-to-country.webhosting.info\/node\/view\/6\">here<\/a>. This is a free database. There are other versions that claim to be more accurate, but they charge a fee. For my purposes, the free version is sufficient.<\/p>\n<p>I first loaded this file into a database and then use a table join to lookup the country code from the IP number. This turned out to be a time-consuming exercise. Then, I remembered that in data warehousing, you want to do as much of your data transformation outside of the database as possible. Applying that principle, I moved the country lookup portion into the perl processing routine prior to loading the data into the database. This move proved to be an excellent time-saver.<\/p>\n<p>Below I show the perl code for matching IP address with country code. There are 3 basic steps:<\/p>\n<p>1. <a href=\"#step1\">Read IP\/Country mapping file<\/a>.<\/p>\n<p>2. <a href=\"#step2\">Convert IP address to IP number<\/a>.<\/p>\n<p>3. <a href=\"#step3\">Find country code based on IP number<\/a><\/p>\n<p>The code for each step is shown below:<\/p>\n<p><a name=\"step1\" title=\"step1\"><\/a><u>1. Read IP\/country mapping file<\/u><\/p>\n<p>open (IN1,&#8217;ip2country.txt&#8217;);<br \/>\n## ip2country.txt is the file that stores the<br \/>\n## IP number\/country mapping data. Assuming the<br \/>\n## following format: IP_START,IP_END,COUNTRY_CODE.<\/p>\n<p>$i=0;<br \/>\nwhile (&lt;IN1&gt;) {<br \/>\nchomp;<br \/>\n@ips = split (&#8220;,&#8221;);<br \/>\n$ip_start[$i] = $ips[0];<br \/>\n$ip_end[$i] = $ips[1];<br \/>\n$ip_country2[$i] = $ips[2];<br \/>\n$i++;<br \/>\n}<\/p>\n<p><a name=\"step2\" title=\"step2\"><\/a><u>2. Convert IP address to IP number<\/u>.<\/p>\n<p>Assume IP address is already stored in the variable $ip_address<\/p>\n<p>@ipp = split (\/\\.\/,$ip_address);<br \/>\n$ip_number = $ipp[0]*256*256*256+$ipp[1]*256*256+$ipp[2]*256+$ipp[3];<\/p>\n<p><a name=\"step3\" title=\"step3\"><\/a><u>3. Find country code based on IP number<\/u><\/p>\n<p>## We want to find the country code where $ip_number is between $ip_start and $ip_end.<\/p>\n<p>while ($ip_number &gt; $ip_end[$j]) {<br \/>\n$j++;<br \/>\n}<br \/>\nif ($ip_number &gt; $ip_start[$j]) {<br \/>\n$country = $ip_country2[$j];<br \/>\n} else {<br \/>\n$country = &#8216;NA&#8217;;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One exercise that you&#8217;d like to do is find out which countries your visitors are coming from. There are several reasons for doing this: 1) You might want to tailor your content to that particular demographic. 2) If you have lots of visitors from a particular country, you might want to consider adding a version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[36,37,38],"_links":{"self":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}