MaxMind Downloadable GeoIP Database PHP Class

MaxMind provides a free downloadable GeoIP database that gets updated the first Tuesday of every month. I have found this data to be incredibly accurate for being free. Some of the data returned from an IP address lookup includes:

  • country code
  • country name
  • region
  • city
  • postal code
  • latitude/longitude
  • area code

To get started with the MaxMind GeoIP look-ups, you will need to download the binary database as well as some include files you will need. Use the link below to get more information + what you need.

Find more here:

http://dev.maxmind.com/geoip/legacy/geolite/

I built this clean and simple PHP class that can help others get started with MaxMind’s service. It just contains one function that gathers all the city data on the ip address and returns it in an array. You could format it whatever way you want after that(json,xml,etc).

<?php

include("geoipcity.inc");
include("geoipregionvars.php");

class MaxMind 
{
    function MaxMindLookup($ip) 
    {
        $gi = geoip_open("/usr/local/share/GeoIP/GeoLiteCity.dat",GEOIP_STANDARD);
        $record = geoip_record_by_addr($gi,$ip);
        $data = array();
        $data['country_code'] = $record->country_code;
        $data['country_name'] = $record->country_name;
        $data['region'] = $record->region;
        $data['city'] = $record->city;
        $data['postal_code'] = $record->postal_code;
        $data['lat'] = $record->latitude;
        $data['lon'] = $record->longitude;
        $data['area_code'] = $record->area_code;
        geoip_close($gi);
        return $data;
    }
}

$obj = new MaxMind;

print_r($obj->MaxMindLookup("74.125.228.96"));


?>

2 Responses to MaxMind Downloadable GeoIP Database PHP Class

  1. An alternative you may consider: http://lite.ip2location.com that provide the information of country, region, city, latitude, longitude, ZIP code and time zone

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>