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

/** 
 * Lookup the country by IP Address
 * This will only fetch country code and country name
 */

include("include/geoip.inc");

$gi = geoip_open("include/GeoIP.dat", GEOIP_STANDARD);

echo "Getting Country Code and Name by IP Address <br /><br />";

$ip = "24.24.24.24";
echo "IP: ". $ip . "<br />";
echo "Country Code: " . geoip_country_code_by_addr($gi, $ip) . "<br />" .
     "Country Name: " . geoip_country_name_by_addr($gi, $ip) . "<br /><br />";

$ip = "80.24.24.24";
echo "IP: " . $ip . "<br />";
echo "Country Code: " . geoip_country_code_by_addr($gi, $ip) . "<br />" .
     "Country Name: " . geoip_country_name_by_addr($gi, $ip) . "<br /><br />";

geoip_close($gi);

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

/**
 * Querying against GeoIP/Lite City
 * This will fetch country along with city information
 */ 

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

$giCity = geoip_open("include/GeoLiteCity.dat",GEOIP_STANDARD);

$ip = "12.215.42.19";
$record = geoip_record_by_addr($giCity, $ip);

echo "Getting Country and City detail by IP Address <br /><br />";

echo "IP: " . $ip . "<br /><br />";

echo "Country Code: " . $record->country_code .  "<br />" . 
	 "Country Code3: " . $record->country_code . "<br />" . 
	 "Country Name: " . $record->country_name . "<br />" . 
	 "Region Code: " . $record->region . "<br />" . 
	 "Region Name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br />" . 
	 "City: " . $record->city . "<br />" . 
	 "Postal Code: " . $record->postal_code . "<br />" .
	 "Latitude: " . $record->latitude . "<br />" . 
	 "Longitude: " . $record->longitude . "<br />" . 
	 "Metro Code: " . $record->metro_code . "<br />" . 
	 "Area Code: " . $record->area_code . "<br />" ;  

geoip_close($giCity);

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

require("include/timezone.php");

echo get_time_zone("US","WV");
echo "<br />";
echo get_time_zone("NP","");

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.

From Mukesh Chapagain's Blog, post PHP MaxMind GeoIP: Get country, city, postal code & much more by IP Address

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS 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
    Thanks
    Suresh kumar

  • http://blog.chapagain.com.np/ Mukesh

    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

  • http://blog.chapagain.com.np/ Mukesh

    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

  • http://blog.chapagain.com.np/ Mukesh

    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..!