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 has been moved to github (didn’t realize it before)

  1. mohamed laouane

    thank you so much , it was simple and easy to do it

    useful article like always , thanks again


  2. php: detecting visitor’s real IP address - pingback on 2014/09/05 at 18:22

Leave a Comment

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

Trackbacks and Pingbacks: