<?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>The html blog &#187; country</title>
	<atom:link href="http://htmlblog.net/tag/country/feed/" rel="self" type="application/rss+xml" />
	<link>http://htmlblog.net</link>
	<description>The web sandbox of Asvin Balloo</description>
	<lastBuildDate>Wed, 07 Oct 2009 17:20:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Geolocate your visitors with PHP (part 1)</title>
		<link>http://htmlblog.net/geolocate-your-visitors-with-php-part-1/</link>
		<comments>http://htmlblog.net/geolocate-your-visitors-with-php-part-1/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 15:04:31 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[country]]></category>
		<category><![CDATA[geoip]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[maxmind]]></category>
		<category><![CDATA[net]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=19</guid>
		<description><![CDATA[

In this short post I'll show you how to geolocate your visitors with PHP. First of all let's have a look at what you'll need :

PEAR GeoIP
MaxMind GeoLite Country


Installing PEAR GeoIP
Using your console or using the command prompt type :
&#160;
pear install http://download.pear.php.net/package/Net_GeoIP-1.0.0RC1.tgz
&#160;
Obtaining MaxMind GeoLite Country
The database can be downloaded freely here. Download it and put [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm3.static.flickr.com/2358/2267796445_29dc34fe87_m.jpg" alt="geolocate" /></p>
<div style="clear:both"></div>
<p>In this short post I'll show you how to geolocate your visitors with <a href="http://php.net">PHP</a>. First of all let's have a look at what you'll need :</p>
<ul>
<li>PEAR GeoIP</li>
<li>MaxMind GeoLite Country</li>
</ul>
<p><span id="more-19"></span><br />
<strong>Installing PEAR GeoIP</strong><br />
Using your console or using the command prompt type :</p>
<pre class="bash">&nbsp;
pear <span style="color: #c20cb9; font-weight: bold;">install</span> http://download.pear.php.net/package/Net_GeoIP<span style="color: #000000;">-1.0</span>.0RC1.tgz
&nbsp;</pre>
<p><strong>Obtaining MaxMind GeoLite Country</strong><br />
The database can be <a href="http://www.maxmind.com/app/geoip_country">downloaded freely here</a>. Download it and put it in the same folder where your script will reside.<br />
<!--more--><br />
<strong>The code</strong></p>
<pre class="php"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Net/GeoIP.php'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$geoip</span> = Net_GeoIP::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'./GeoIP.dat'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$ip</span> = <a href="http://www.php.net/getenv"><span style="color: #000066;">getenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'REMOTE_ADDR'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$country</span> = <span style="color: #0000ff;">$geoip</span>-&gt;<span style="color: #006600;">lookupCountryName</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ip</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Hi, you're from &quot;</span>.<span style="color: #0000ff;">$country</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p><strong>Walkthrough</strong><br />
Now let's go through the lines of code. The first line  tells us to include the GeoIP class, which we installed previously and then create a geoip object in line 5 from the database we downloaded.<br />
Next, in line 6 we get the IP address of the visitor and finally we call the <a href="http://pear.php.net/manual/en/package.networking.net-geoip.lookupcountryname.php">lookupCountryName</a> method with the IP address as parameter in line 7 and it will return us the country. </p>
<p>The last line just outputs the country, for example,  "Hi, you're from Mauritius". That's it, now you know where your visitor's from.</p>
<p><a href="http://htmlblog.net/geolocate-your-visitors-with-php-part-2/">In the second part of this article</a> I'll show you how to use the script to visually map your visitors using <a href="http://code.google.com/apis/maps/">Google Maps</a>. You can have a little <a href="http://htmlblog.net/demo/myip/">preview of the script here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/geolocate-your-visitors-with-php-part-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
