Geolocate your visitors with PHP (part 1)
Filed under PHP
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 :
pear install http://download.pear.php.net/package/Net_GeoIP-1.0.0RC1.tgz
Obtaining MaxMind GeoLite Country
The database can be downloaded freely here. Download it and put it in the same folder where your script will reside.
The code
<?php
require_once('Net/GeoIP.php');
$geoip = Net_GeoIP::getInstance('./GeoIP.dat');
$ip = getenv('REMOTE_ADDR');
$country = $geoip->lookupCountryName($ip);
echo "Hi, you're from ".$country;
?>
Walkthrough
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.
Next, in line 6 we get the IP address of the visitor and finally we call the lookupCountryName method with the IP address as parameter in line 7 and it will return us the country.
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.
In the second part of this article I’ll show you how to use the script to visually map your visitors using Google Maps. You can have a little preview of the script here.
Jul09









July 9, 2008 at 11:24 am
This is cool & easily tweakable.
Thanks for providing the info online.
Here’s another one you may want to check out too. – http://www.ardamis.com/2008/01/27/how-to-geolocate-visitors-using-an-ip-to-country-database/
Goodnight :)
July 19, 2008 at 2:26 am
You can also use my IP2C library.
it comes with a binary database file, and performance is excellent.
http://firestats.cc/wiki/ip2c
July 19, 2008 at 11:24 am
thnx for the tip Omry
July 21, 2008 at 6:08 pm
[...] his blog today Asvin Balloo has posted the first part of his look at his method for geolocating the visitors to your website using the PEAR GeoIP package [...]
July 22, 2008 at 2:38 pm
[...] Geolocate your visitors with PHP (part 1) (0 visite) Tags: geolocalisation [...]
July 23, 2008 at 10:07 am
[...] PEAR GeoIP – PHP Library fürs automatische orten (Beispiel) [...]
July 24, 2008 at 12:16 am
[...] Geolocate your visitors with PHP (part 1) (0 visite) [...]
July 30, 2008 at 3:30 pm
[...] In the first part of this series I showed how you could get the country of a visitor via his IP address. Now with this precious information, I’ll show you how to map the visitor visually on the world map using Google Maps. [...]
July 31, 2008 at 12:37 pm
[...] Das HTML Blog hat ein sehr gutes Tutorial zu diese Methode und PHP. [...]
November 3, 2008 at 12:44 pm
We are not very well-versed in programming. Therefore, we are using the PHP API from the IP2Location.
http://www.ip2location.com/php.aspx
December 14, 2008 at 8:19 pm
[...] en AWStats Saved by weelassie on Wed 10-12-2008 Geo tv live Saved by FSTOP on Sat 06-12-2008 Geolocate your visitors with PHP (part 1) Saved by sakurachan19 on Sun 23-11-2008 IE6 and Google Maps API Saved by snekse on Wed 15-10-2008 [...]
February 28, 2009 at 3:12 pm
[...] A library that uses Maxmind’s GeoIP databases to accurately determine geographic location of an IP address. Tutorial from HTML Blog [...]
May 29, 2009 at 9:12 am
[...] A library that uses Maxmind’s GeoIP databases to accurately determine geographic location of an IP address. Tutorial from HTML Blog [...]
May 30, 2009 at 9:01 pm
[...] PEAR GeoIP – PHP Library fürs automatische orten (Beispiel) [...]
June 5, 2009 at 9:05 am
[...] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Friday, June 5th, 2009 at 10:35 am and is filed under Php Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]
November 5, 2009 at 10:31 pm
[...] A library that uses Maxmind’s GeoIP databases to accurately determine geographic location of an IP address. Tutorial from HTML Blog [...]
July 18, 2010 at 8:46 am
great tutorial, helps me a lot. thank you for it