<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>phoenixheart - portfolio &#38; more &#187; ip</title> <atom:link href="http://www.phoenixheart.net/tag/ip/feed/" rel="self" type="application/rss+xml" /><link>http://www.phoenixheart.net</link> <description>phoenixheart - portfolio &#38; more</description> <lastBuildDate>Wed, 25 Aug 2010 03:02:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <script type="text/javascript">/*<![CDATA[*/if(typeof Meebo=="undefined"){Meebo=function(){(Meebo._=Meebo._||[]).push(arguments)};(function(q){var args=arguments;if(!document.body){return setTimeout(function(){args.callee.apply(this,args)},100);}var d=document,b=d.body,m=b.insertBefore(d.createElement('div'),b.firstChild);s=d.createElement('script');m.id='meebo';m.style.display='none';m.innerHTML='<iframe id="meebo-iframe"></iframe>';s.src='http'+(q.https?'s':'')+'://'+(q.stage?'stage-':'')+'cim.meebo.com/cim/cim.php?network='+q.network;b.insertBefore(s,b.firstChild);})({network:'phoenixheartnet_bo16we'});}/*]]>*/</script> <item><title>IP address to geographic details</title><link>http://www.phoenixheart.net/2009/01/ip-address-to-geographic-details/</link> <comments>http://www.phoenixheart.net/2009/01/ip-address-to-geographic-details/#comments</comments> <pubDate>Tue, 06 Jan 2009 08:33:08 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[geo]]></category> <category><![CDATA[geographic]]></category> <category><![CDATA[ip]]></category> <category><![CDATA[php]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=204</guid> <description><![CDATA[Today I received a small project to detect the country of web visitors. I&#8217;ve never done something like that before, so as always, I started googling around. And it turned out that it&#8217;s not that complex &#8211; there is a company called MaxMind that offers APIs to look up geographic details from an IP address. The [...]]]></description> <content:encoded><![CDATA[<p><span
class="drop-cap">T</span>oday I received a small project to detect the country of web visitors. I&#8217;ve never done something like that before, so as always, I started googling around. And it turned out that it&#8217;s not that complex &#8211; there is a company called <a
href="http://www.maxmind.com">MaxMind</a> that offers <a
href="http://www.maxmind.com/app/api">APIs</a> to look up geographic details from an IP address. The APIs are written in a variety of languages like C, Perl, Java, Python, Ruby, C#, VB.NET, Pascal, Javascript, and of course, PHP.</p><p>For this project, I decided to go with PHP version which has a <a
href="http://pear.php.net/package/Net_GeoIP/">NET_GeoIP PEAR package</a>. Just download it, along with the free data files (they have paid ones also) &#8211; <a
href="http://www.maxmind.com/app/geolitecountry">Geo Lite Country</a> if you want the country details only, or <a
href="http://www.maxmind.com/app/geolitecity">Geo Lite City</a> if you want extra stuffs like city, region, latitude and longtitude etc. In this post, I&#8217;ll go with City version.<span
id="more-204"></span></p><p>Now, with everything downloaded and extracted into a folder, start with an empty index.php file and fill it with some code:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;path/to/file/GeoIP.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define a new instance of Net_GeoIP class</span>
<span style="color: #000088;">$geoip</span> <span style="color: #339933;">=</span> Net_GeoIP<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path/to/file/GeoLiteCity.dat&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lookupLocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Handle exception</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>The function lookupLocation() from class NET_GeoIP accepts a string parameter in IP format &#8211; something like &#8220;210.245.17.13&#8243; and returns a Location object which contains several useful geographic information. If you run this script on your localhost however, nothing will be printed. That is because in such a case, $_SERVER['REMOTE_ADDR'] is a local IP &#8211; most often 127.0.0.1 &#8211; which cannot tell us anything. Have you heard this quote &#8220;There is nothing like 127.0.0.1&#8243;?</p><p>So for testing purpose, let&#8217;s modify the above code a bit, so that it can accept an optional query parameter as an IP.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;path/to/file/GeoIP.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define a new instance of Net_GeoIP class</span>
<span style="color: #000088;">$geoip</span> <span style="color: #339933;">=</span> Net_GeoIP<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path/to/file/GeoLiteCity.dat&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'test_ip'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'test_ip'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lookupLocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Handle exception</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>Now if you&#8217;ve set it everything correctly in place, browse to index.php?test_ip=210.124.12.3 will show something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">object<span style="color: #009900;">&#40;</span>Net_GeoIP_Location<span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">#2 (10) {
</span>  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;countryCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;KR&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;countryName&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">18</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;Korea, Republic of&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;region&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;11&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;city&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;Seoul&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;postalCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  <span style="color: #009900; font-weight: bold;">NULL</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;latitude&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  float<span style="color: #009900;">&#40;</span><span style="color:#800080;">37.5664</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;longitude&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  float<span style="color: #009900;">&#40;</span><span style="color:#800080;">126.9997</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;areaCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  <span style="color: #009900; font-weight: bold;">NULL</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;dmaCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  <span style="color: #009900; font-weight: bold;">NULL</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;countryCode3&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;KOR&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Similarly, index.php?test_ip=128.54.13.8 shows</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">object<span style="color: #009900;">&#40;</span>Net_GeoIP_Location<span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">#2 (10) {
</span>  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;countryCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;US&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;countryName&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;United States&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;region&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;CA&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;city&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;La Jolla&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;postalCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;92093&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;latitude&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  float<span style="color: #009900;">&#40;</span><span style="color:#800080;">32.8807</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;longitude&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  float<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">117.2359</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;areaCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  int<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">858</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;dmaCode&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  float<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">825</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;countryCode3&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span>
  string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;USA&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Extremely useful, isn&#8217;t it? With these information on hand, there&#8217;s no limit of what we can do to improve user interaction and much more.</p><p>A simple demo of this can be found <a
href="http://www.phoenixheart.net/projects/countrydetector">here</a>.</p> <img
style='display:none' id="post-204-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/01/ip-address-to-geographic-details/',title:'IP address to geographic details',tweet:'Today I received a small project to detect the country of web visitors. I&#8217;ve never done somet',description:'Today I received a small project to detect the country of web visitors. I&#8217;ve never done somet'})"><script type='text/javascript'>document.getElementById("post-204-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/01/ip-address-to-geographic-details/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 5/11 queries in 0.018 seconds using disk

Served from: www.phoenixheart.net @ 2010-09-09 08:08:15 -->