Tag Archives: maxmind

Use Maxmind Free GeoLiteCountry for geo targeting visitors (example)

So, I should put it here for my personal notes, in case I lost my notes.
Maxmind provide us, developer, a free geo IP country database. This database is useful for geo targeting visitors. For example you can show special message for visitors coming from certain country or maybe blocking visitors that coming from certain country. Ads targeting is also one of it’s usage.
Here’s step by step guide for Maxmind GeoLiteCountry :

Sample PHP code usage:


$special_country=array("US", "UK", "CA", "AU");

$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$countrycode=geoip_country_code_by_addr($gi, $remoteip);
$dx=array_search($countrycode, $special_country);
if ($dx!==FALSE) {
    //do something for visitors coming from $special_country array, i.e. United States, United Kingdom, Canada, Australia

Notes: this free database is updated monthly. Usually every first week of each month, so make sure you download the latest GeoIP.dat.gz file.

updated: Maxmind’s geoip.inc has been moved to github (didn’t realize it before)