Skip to content

February 17, 2011

IP Addresses and Geolocation – Using GeoIP

Did you ever wonder how some sites are able to tailor a message specifically to your city?  You might have seen these in ads: “Albuquerque mom loses 50 lbs!”  Hmm.  How did the site know you live in Albuquerque?

The answer is geolocation based on the user’s IP address.  And of course, geolocation can be useful beyond tailoring custom ad messages.  The good news?  Free geolocation functionality exists!

I’ve been investigating the GeoIP product from MaxMind.  They’ve got a free version of their geolocation database as well as free APIs in C, perl, PHP, Java, Python, C#, Ruby, and several others.  You can import their geolocation database into MySQL or other relational database for querying, or use the APIs (this latter option is recommended).  I chose to use the geoip PECL package for PHP (documented here).

Once installed, the geoip package provides over a dozen PHP functions for accessing geolocation information by IP address or host name.  The function you’ll probably be most interested in is geoip_record_by_name ( string $hostname ).

Consider the code:

$ipAddress = $_SERVER['REMOTE_ADDR'];

This will output an associative array that will look something like:

    [continent_code] => NA
    [country_code] => US
    [country_code3] => USA
    [country_name] => United States
    [region] => TX
    [city] => Houston
    [postal_code] => 77002
    [latitude] => 29.7523002625
    [longitude] => -95.3669967651
    [dma_code] => 618
    [area_code] => 713

You can now take this data and customize your site’s user experience based on the location of the user, with one important caveat: no geolocation database is 100% accurate, and the free GeoIP city database from MaxMind is less accurate than their pay version.  So, you must not rely on this data for critical functionality.

What have I done with IP address geolocation?

I’ve begun using it to promote US state quizzes to visitors of GoToQuiz.  There is at least one quiz for every US state.  I’m in the process of selecting which one I like best for each and promoting it to users based on their home state, with a little help from GeoIP.

Using my newly-created state flag icons, a link to the quiz of a user’s own state will display at the bottom of a quiz results page, like so:

state quiz example

The idea is to catch the eye of the visitor with the correct link text and state flag, so that he or she will continue to explore GoToQuiz.

In the array above, returned by GeoIP, you can see that the “region” key returns the two-letter state code for US states.  That is what I am interested in for this application.  Some PHP code:

if (function_exists('geoip_record_by_name')) {
    $city = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
    if ($city && $city['country_code'] == 'US' && $city['region']) {
        $stateq = StateQuizMap::lookupQuiz($city['region']);
        if ($stateq) {
            // code for displaying link omitted

On Line 1 I check to ensure the geoip function exists.  Then I call it to get the data based on the user’s IP address.  Because the GeoIP database is incomplete, I check to see whether it has the appropriate data set, as well as to see if it is a US record.  If so, on Lines 3 through 5 I use another class, called StateQuizMap, to look up the desired state quiz based on the value of $city[‘region’] (which is the two-letter state code).  If such a quiz is returned, I display the link. Voilà!

You can probably think of many great ideas to implement with geolocation.  Happy coding!

Read more from PHP

Share your thoughts, post a comment.


Note: HTML is allowed. Your email address will never be published.

Subscribe to comments