GeoIP Country Location – PHP Script

Basically, this PHP script utilizes MaxMind‘s free database for determining the country of an IP address.

Download:

Step 1) Get country-redirect.php

Step 2) Get the most recent IP-country database.

Step 3) Extract, then upload both files to the same directory.

Demo: You may see where it thinks you are from.

Once you download the script, go to the end of country-redirect.php to customize its output (here’s an example of how to make it redirect).

More information

I’m not offering support for this script. The PHP coding wasn’t written entirely by me.  I essentially combined a bunch of classes from MaxMind to make this work. I do not guarantee its functionality on your server. It should work for PHP4-5 at least.

The actual determining of the country is about 99.5% accurate.  All AOL users are assumed to be from the United States.

In my case, I find this is helpful for affiliate offers that redirect based on a user’s country. This way (if you are an affiliate), you can select to which offers you’d like to send your international visitors.

Anyway, this should be helpful for providing more relevant content to all of your users.

Was this free article helpful? Share it!








19 comments

  1. Craig m

    I’s love to see more stuff written about showing the city also.. There’s a few free scripts that show city also..

  2. Floris

    Hi Mike,

    I got a blank page as wel. My $_SERVER['REMOTE_ADDR'] variable was a local/network address (192.168. etc). So it could not resolve it’s country, duh! Be sure to run the script with a public address or simply upload the script to a webserver so it receives your public ip-address.

    Floris

  3. Dan

    I would just like to mention that although I give you kudos for effort, your code is very sloppy and no commenting or documentation. Very poor implementation of your work sir.

  4. Mike

    @Dan: If you read under “more information” you would have noticed that I wrote that I hardly programmed any of this. It’s a compilation of classes. If you’d like to improve the code and documentation for this free script, go ahead. The other users would likely appreciate it.

  5. Seb

    Hey Mike,

    Thanks for your script. It’s working like a charm!
    I would like to redirect only UK and US visitors to different pages. The rest of them should continue with the current page.

    What exactly should I add in the code?

    Cheers,
    Seb

  6. vnmilen

    Thank, It wokrs great!

    “Seb”, look for the line #368
    1) return $gi->GEOIP_COUNTRY_NAMES[$country_id]; //if you want the contry’s name
    2) return $gi->GEOIP_COUNTRY_CODES[$country_id]; //if you want the contry’s code

    If you have your script, just write:
    include(“country-redirect.php”);
    and the script will write the code or the name of the country, but if you want something else, just replace the line #503 (echo $country;) with something other.

    If you have questions, find my e-mail on my site:
    http://www.atanasov.co.cc/index.php

  7. Sahil

    Can this script be shortened so as to block only 1 country. It is 25k in size, so this is why for 1 country it could be only a few kb.

  8. Sahil

    One more thing. If a country is disallowed / redirected, can a specific IP be allowed from that country?

    I would really appreciate if someone could help me with the code I need to put at line 503 for this.

    Thanks!

  9. vnmilen

    If you will use this scripts for redirect users to other page of your site, think about that the Google BOTS are also “users” and also they will be redirected, and in that way, your site will not be indexed in Google and you will have got an unbelevable big “trafic” to your site, because they will index sites that not exacts!

    If you have questions, find my e-mail on my site (english, french, bulgarian):
    http://www.atanasov.co.cc/en/
    http://www.atanasov.co.cc/fr/
    http://www.atanasov.co.cc/

Add your comment