GeoIP with MaxMind’s GeoLite in PHP

You can download the GeoLite Country database from GeoLite Free Downloadable Databases and you can grab the MaxMind GeoIP PHP API.

Once you have the GeoIP.dat file and the geoip.inc PHP file you’re good to go. E.g.:


  require_once __DIR__ . '/geoip/geoip.inc';

  $gi = geoip_open( __DIR__ . '/geoip/GeoIP.dat", GEOIP_STANDARD );

  $country_code = geoip_country_code_by_addr( $gi, $host );
  $country_name = geoip_country_name_by_addr( $gi, $host );

  geoip_close( $gi );