Home » PHP2 August 2011

PHP MaxMind GeoIP: Get country, city, postal code & much more by IP Address

This article describes how to get country and city information using MaxMind‘s GeoIP technology. You provide the IP address of the visitor and GeoIP can give you country and city information for that IP address.

About MaxMind

Founded in 2002, MaxMind is an industry-leading provider of geolocation and online fraud detection tools. MaxMind provides its geolocation technology through the GeoIP brand.

What is GeoIP?

GeoIP® is the proprietary technology that drives MaxMind’s IP geolocation data and services. GeoIP provides businesses with a non-invasive way to determine geographical and other information about their Internet visitors in real-time. When a person visits your website, GeoIP can determine which country, region, city, postal code, and area code the visitor is coming from. Furthermore, GeoIP can provide information such as longitude/latitude, connection speed, ISP, company name, domain name, and whether the IP address is an anonymous proxy or satellite provider.

They have both free and paid service of GeoIP. This article uses the free version of GeoIP.

Get country name and country code by IP address

You need to include two files: geoip.inc and GeoIP.dat

Download: geoip.inc | GeoIP.dat

Get Country and City detail by IP address

You can get Country, Region, City name and code, Postal code, Latitude, Longitude, Metro code and Area code by IP address.

You need to include three files: geoipcity.inc, geoipregionvars.php and GeoLiteCity.dat

Download: geoipcity.inc | geoipregionvars.inc | GeoLiteCity.dat

Get timezone by country and region code

You can get timezone for any country using the country code. You can also use region code for getting region wise timezone as well.

You need to include one file: timezone.php

Download: timezone.php

You can download the php api with sample code files from Maxmind’s GitHub repo: https://github.com/maxmind/geoip-api-php

Hope it helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Contact

    Thanks. :)

  • Prabhu

    the above code save in which file? what is name ll be given for that

  • paul

    can the above instructions be followed for installation in magento?

  • Suresh kumar

    could you please suggest me how to find timezone.php to include in my own script
    Suresh kumar

  • For this purpose, I have created a Magento module named ‘Auto Currency’. You can download the module from Magento Connect: http://www.magentocommerce.com/magento-connect/auto-currency-switcher-8671.html

  • Previous timezone.php link was not working. I have updated the post. You can download it from Maxmind’s GitHub Repo: https://github.com/maxmind/geoip-api-php/tree/master/src

  • You can make a folder named ‘geoip’ in your website’s root folder. There you can create any php file and write the code. You have to create another folder named ‘include’ inside ‘geoip’ folder and put geoip.inc, geoipcity.inc, geoipregionvars.php, timezone.php inside it.

  • Tushar

    Hello Mukesh,

    As I observed this code and DB is not working for all “IPs” in the world.
    If I purchase the DB from maxmind then these relevant files (geoipcity.inc, geoipregionvars.php) will work ?? if not then where I can get these files..!