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.

Share this:

Comments

40 responses to “GeoIP Country Location – PHP Script”

  1. Craig m Avatar
    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. Mike Avatar
    Mike

    @craig m: Yeah, very true. I was keeping this script for a single purpose, though.

  3. sana Avatar
    sana

    hy i install but it showes me blank page 🙁

  4. sana Avatar
    sana

    its working now.i just upload GeoIP.dat file as binary mode. Thanks for this 🙂

  5. sentih Avatar

    sorry, how redirect to a page?

  6. Mike Avatar
    Mike

    @sentih: Here’s detailed instructions on how PHP redirects work: http://us2.php.net/manual/en/function.header.php

  7. Sam Avatar
    Sam

    Can this be made to only look at the United States and geocode the States like Colorado, Florida, Etc. and send the users to a sub domain name if they in Colorado they would be sent to http://co.mydomain.com/

  8. Mike Avatar
    Mike

    Hi, i installed but a blank page is being shown.

  9. Floris Avatar
    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

  10. Dan Avatar
    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.

  11. Mike Avatar
    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.

  12. Mike Avatar
    Mike

    @Sam: It is possible. Creating automatic subdomains would be quite difficult though.

  13. Seb Avatar
    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

  14. Shock Marketer Avatar
    Shock Marketer

    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.

    1. Herve Avatar
      Herve

      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

  15. vnmilen Avatar

    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

    1. Rajni Avatar
      Rajni

      Hi, i installed but a blank page is being shown.
      how to upload GeoIP.dat file as binary mode?

  16. Sahil Avatar
    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.

  17. Sahil Avatar
    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!

  18. vnmilen Avatar

    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/

  19. Tanmay Avatar
    Tanmay

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

  20. Aliarth Avatar

    Thanks a lot!!! That script wery usefull for domain poiners redirecting to other language sites. 🙂

  21. Rach04 Avatar
    Rach04

    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?

    1. Shock Marketer Avatar
      Shock Marketer

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

      1. Rach04 Avatar
        Rach04

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

        1. Shock Marketer Avatar
          Shock Marketer

          Paste the code you are using here and I’ll take a look.

          1. Rach04 Avatar
            Rach04

            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);

          2. Shock Marketer Avatar
            Shock Marketer

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

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

          3. Rach04 Avatar
            Rach04

            i made the adjustment and it still goes to the default url.

          4. Shock Marketer Avatar
            Shock Marketer

            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.

          5. Rach04 Avatar
            Rach04

            i guess thats the problem, it is giving me a blank white page.

          6. Rach04 Avatar
            Rach04

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

          7. Shock Marketer Avatar
            Shock Marketer

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

  22. Jason Avatar
    Jason

    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.

  23. F8LERROR Avatar
    F8LERROR

    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.

    1. F8LERROR Avatar
      F8LERROR

      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.

      1. Shock Marketer Avatar
        Shock Marketer

        Glad it worked for you. There’s free proxies out there, but quite a few are abused and slow.

  24. razvan Avatar

    i cannot make it work, i put the php in root directory and still nothing, it tooks me to the default url plz help

  25. Claude "CodeAngry" Adrian Avatar

    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) 😉

  26. Bruno Amorim Avatar

    Thanks a lot! It works perfectly…

Leave a Reply

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