Where am I? PHP, Pear, Net_GeoIP and Me: Geo-location by IP

Legalese: “This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com/.”

The mission: show our local (in the meastspace sense) visitors a different page from the folks further away. We wanted to show off our store, and attract people to stop in and sign up for photography seminars and classes. (Memo to marketing: perhaps we should measure how effective all this is).  And not annoy our pals in the U.K. who could care less about our coffee bar.

Where to begin? We’re in a state of flux with all of our servers, so we ponied up $250 for an old Dell Poweredge 2500 Server.  We did an Internet installation of Fedora Core 9, because our production and development servers haven’t been updated in a long, long time. Sure, we’d compiled the LAMP basics (from source, for reasons never made clear – perhaps “Because We Could” is the answer), but other than that – well, if it wasn’t broke, we didn’t fix it.

First off, I didn’t know anything about Pear. Luckily I found this article at techrepublic to help me put the pieces together. So Pear is kinda sorta like a cpan tool, but for php instead of Perl. Some re-usable components and so forth.

Install and Configure pear

Anyway, I didn’t have pear installed on my FC9 box. How to install pear? Easy:

yum install php-pear

That was easy. Next, I need to tell php where it can find the pear components I am about to install. Edit /etc/php.ini, and you’ll need to add /usr/share/pear to php’s include_path:

; Paths and Directories ;

; UNIX: "/path1:/path2"
include_path = ".:/usr/share/pear"

Install Net_GeoIP

pear install Net_GeoIP

My installation complained that there was no “stable version” of Net_GeoIP, and that I should instead download the beta version. Your mileage may vary. I had to do this:

pear install channel://pear.php.net/Net_GeoIP-1.0.0RC1

Of interest: pear’s modules are placed in a structure that is kind of like Java’s com/domain/class structure.  In this case the _ (underscore) becomes a directory separator. That is, to use Net_GeoIP, I need to write this require_once line in my php program:


Grab a Location Database

I grabbed the City database from http://www.maxmind.com/app/geolitecity. Please note there are licensing and restrictions of its use. There are both commercial and free versions. Be sure to properly cite MaxMind as per their web site. Note: the file is big. If you are developing using a Windows PC as a middleman, I suggest you wget the database from the Linux command line right into your web server’s filesystem.

I created a subdirectory in my web site’s filesystem called “GeoIP,” into which I copied the GeoLiteCity.dat file.

Program 1: geoTest.php Output

Click here to try out the program.  It will tell you roughly how far you are from this server.


Listing 1: geoTest.php

Click here for the program listing. You should be able to just cut and paste the code and with relatively few modifications have a running version of your own.