// you’re reading...

PHP

Geolocate your visitors with PHP (part 1)

geolocate

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

  1.  
  2. <?php
  3. require_once('Net/GeoIP.php');
  4.  
  5. $geoip = Net_GeoIP::getInstance('./GeoIP.dat');
  6. $ip = getenv('REMOTE_ADDR');
  7. $country = $geoip->lookupCountryName($ip);
  8.  
  9. echo "Hi, you're from ".$country;
  10. ?>
  11.  

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Mixx
  • Ma.gnolia
  • Technorati
  • Netvouz
  • Reddit
  • Propeller
  • Slashdot
  • description

Discussion

10 comments for “Geolocate your visitors with PHP (part 1)”

  1. 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 :)

    Posted by Shah | July 9, 2008, 11:24 am
  2. You can also use my IP2C library.
    it comes with a binary database file, and performance is excellent.

    http://firestats.cc/wiki/ip2c

    Posted by Omry | July 19, 2008, 2:26 am
  3. thnx for the tip Omry

    Posted by asvin | July 19, 2008, 11:24 am
  4. [...] 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 [...]

    Posted by Asvin Balloo’s Blog: Geolocate your visitors with PHP (part 1) | Development Blog With Code Updates : Developercast.com | July 21, 2008, 6:08 pm
  5. [...]  Geolocate your visitors with PHP (part 1) (0 visite) Tags: geolocalisation [...]

    Posted by Geolocalisation en PHP - To take away | July 22, 2008, 2:38 pm
  6. [...] PEAR GeoIP - PHP Library fürs automatische orten (Beispiel) [...]

    Posted by » Benutzer geographisch orten Flusensieb | July 23, 2008, 10:07 am
  7. [...]  Geolocate your visitors with PHP (part 1) (0 visite) [...]

    Posted by Geolocalisation en PHP | July 24, 2008, 12:16 am
  8. [...] 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. [...]

    Posted by The html blog | Geolocate your visitors with PHP (part 2) | July 30, 2008, 3:30 pm
  9. [...] Das HTML Blog hat ein sehr gutes Tutorial zu diese Methode und PHP. [...]

    Posted by Dynamische Signatur - e-Schrecking für Foren, MySpace, … | Technik, Gothic und Anderes | July 31, 2008, 12:37 pm
  10. We are not very well-versed in programming. Therefore, we are using the PHP API from the IP2Location.

    http://www.ip2location.com/php.aspx

    Posted by Tim | November 3, 2008, 12:44 pm

No Comments yet, your thoughts are welcome