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.

Make sure you get my free newsletter because best practices change as we find new learnings and discoveries. Keep up to date:

Was this free article helpful? Share it!

40 thoughts on “GeoIP Country Location – PHP Script

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

  2. 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. 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. @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. 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. Go to line 503 of “country-redirect.php” and replace “echo $country;” with:
    if($country == 'UK')
    {
    header('Location: http://example.co.uk');
    exit();
    }
    else if($country == 'US')
    {
    header('Location: http://example.com');
    exit();
    }

    That should work – although I haven’t used this script in quite a while.

    • i like the script, i tested it, everything is fine, but i want to know how to do if i want to redirect people in many different countries, for example in UK, USA, CANADA, FRANCE, BELGIUM….and any other.

      Thanks

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

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

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

  10. 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/

  11. Thanx for this… I have used this reference to develop drupal module GeoRedirect. will be contribute it soon… 🙂

  12. I’m working on a website redirect and when I input the code for redirects, it doesn’t seem to redirect correctly. It tends to go to the default case in all instances. any ideas?

    • If it’s going to the default example.com URLs, then you need to change the URLs to your website.

      • i did change the urls to the website i needed, but no matter what the case it goes straight to the default case.

          • i am using the country -redirect code provided and then just changed the last part of the code….

            $gi = geoip_open(‘GeoIP.dat’, GEOIP_MEMORY_CACHE);
            $country = geoip_country_code_by_addr($gi, $_SERVER[‘REMOTE_ADDR’]);
            // prints the country code your visitor is in
            //echo $country;
            // the end

            if($country == ‘US’)
            {header (‘Location: http://shop.viewsonic.com‘);exit();}
            else ($country != ‘US’)
            {header (‘Location: http://yahoo.com‘);
            exit();}
            geoip_close($gi);

          • I see the error. You are missing “if” after “else”.
            Instead of:
            else ($country != 'US')

            It should be:
            else if ($country != 'US')

          • Okay, then it’s not finding that the country is “US.”

            Delete those conditional statements at the end and just have this code:
            echo $country;

            This will give us an idea if the script works with your server.

          • i got the script to state that i was n the US, however, i still can’t get it to redirect

          • Delete the portion that prints “US”. And add the redirect/header code back. You can paste the code here if you still have problems.

  13. This post may be old, but it still works great! Thank you. Simplest instructions anywhere, by far. Much better than even maxmind’s own GeoIP instructions.

  14. I know this is an old thread. But has anyone tested this with Drupal? I only ask because I cannot seem to figure out how to get the modules that are supposed to do country based redirection to work and the documentation on them is very lacking (at least what I can find).

    Also is there any way to make this redirect everyone outside the USA to a specific page and only USA people to see the actual site for now? Later after I have the other sites set up I would like to have the specific countries redirected to their specific site.

    • I think I have it figured out, now I just have to have a buddy outside the USA test it for me. I tried a proxy but it was so freaking slow that it timed out.

  15. I recently published a self-contained (can be plugged into any framework, web-platform, bloggin-engine) PHP 5.3+ script with a self-updatable database that resolves both IPv4 and IPv6 addresses to their respective country codes.

    Take a look at ipcountryphp.com. It’s of great use to SEOs / SEMs (regardless of hat) 😉

Leave a Reply

Your email address will not be published. Required fields are marked *