1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

GeoIP Redirect Script /w Maxmind Free Database (non-JS version)

Discussion in 'Making Money' started by mointernet, Nov 21, 2011.

  1. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    315
    Likes Received:
    151
    This is the non-javascript version of the GeoIP redirect script and I take no credit for the codes below as I believed similar script were posted very long ago but you know peeps here do not search that far back so I hope this will help some people here. I personally run thousands of traffic daily thru this script using media buys with no problems, the only maintenance you need is to manually update the Maxmind database file monthly and of course edit the country codes to your traffic requirements.

    The advantage of using this here other than for the purpose of redirecting traffic according to country codes, this script uses php codes (non JS) and read its database through a flat file database (no mySQL) available for free and you can update it monthly (Maxmind website). Not only its fast and its pretty accurate.


    copy & save (edit and add country codes where necessary) this code as a geoip.php file. note: The default line should be there all times as it redirect all else traffic
    Code:
    <?php
    
    include("geoip.inc");
    $ip=$_SERVER['REMOTE_ADDR'];
    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
    
    $country_code = geoip_country_code_by_addr($gi, "$ip");
    
    // Country name is not used so commented
    // Get Country Name based on source IP
    //$country = geoip_country_name_by_addr($gi, "$ip");
    
    geoip_close($gi);
    
    switch($country_code)
    
        {
            case "US": header("Location: http://yourUSAofferURL.com"); break;
            case "CA": header("Location: http://yourCAofferURL.com"); break;
            case "GB": header("Location: http://yourUKofferURL.com"); break;
            default: header("Location: http://yourINTLofferURL.com");
    }
    ?>
    

    download & unzip the GeoIP.dat file from the URL (you should manually update this monthly for accuracy).
    Code:
    http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/
    

    unzip geoip.zip (attachment) and place geoip.inc, GeoIP.dat and geoip.php together and send your traffic through the geoip.php file. **You may rename geoip.php to whatever filename you like.
     

    Attached Files:

    • Thanks Thanks x 43
  2. golk75

    golk75 Regular Member

    Joined:
    Jul 25, 2010
    Messages:
    228
    Likes Received:
    25
    Occupation:
    Here
    Location:
    Top of the World
    Nice share, thanks!
     
  3. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    Thanks mate for the php code. I already use MaxMind in Delphi, and it's good to have it for PHP too!
     
  4. manolo12399

    manolo12399 Senior Member

    Joined:
    Jan 3, 2009
    Messages:
    819
    Likes Received:
    161
    Is it possible to run the script from inside, my index page?

    I tried with a script like your and I did not manage to make it work, so I used the javascript script.

    Would be better to advert your domain, has
    Code:
    www.domain.com
    than
    Code:
    www.domain.com/whatever.php
    At least for my need.

    Thanks
     
  5. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    315
    Likes Received:
    151
    save the php code to index.php and place geoip.inc and GeoIP.dat in your root directory. You can redirect your traffic through www.domain.com without typing index.php or whatever.php.
     
    • Thanks Thanks x 2
  6. jairathnem

    jairathnem Power Member

    Joined:
    Oct 27, 2010
    Messages:
    550
    Likes Received:
    316
    Occupation:
    Student
    Location:
    Incredible India!
    Home Page:
    How do i add it to wordpress?
     
  7. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    315
    Likes Received:
    151
    this is a standalone script so it might need some mods to make it work with wordpress. It will be better to find a Geo plugin for WP if there is any out there.
     
  8. manolo12399

    manolo12399 Senior Member

    Joined:
    Jan 3, 2009
    Messages:
    819
    Likes Received:
    161
    Thanks a lot I am going to implement straight away.


    UPDATE: work perfect.

    Thanks.
     
    Last edited: Nov 22, 2011
  9. pandanelu

    pandanelu Newbie

    Joined:
    Nov 12, 2011
    Messages:
    39
    Likes Received:
    2
    could anyone help me and tell me how to use it? thank you
     
  10. Essence19

    Essence19 BANNED BANNED

    Joined:
    Aug 10, 2010
    Messages:
    830
    Likes Received:
    352
    I was looking for it the whole of yest and now i see you posted it here.lol thanks and rep.it works great.
     
  11. 4bothis

    4bothis Junior Member

    Joined:
    Feb 28, 2010
    Messages:
    152
    Likes Received:
    28
    thanks so much for this. been trying to filter out all these pesky indian traffic from my pages.
     
  12. runninggamingbot

    runninggamingbot Newbie

    Joined:
    Sep 22, 2009
    Messages:
    4
    Likes Received:
    0
    man this script is one of the most usefull script ive found here. Very nice job!
     
  13. runninggamingbot

    runninggamingbot Newbie

    Joined:
    Sep 22, 2009
    Messages:
    4
    Likes Received:
    0
    same for me man lol i was looking at that geoip.zip and was trying to understand how the hell that thing was working there something missing... than after 30min i find out this thread with all explaination!! haha
     
  14. NilssonCPA

    NilssonCPA Junior Member

    Joined:
    Jun 18, 2011
    Messages:
    103
    Likes Received:
    25
    Location:
    SWEDEN
    just name the php file index.php?
     
  15. manolo12399

    manolo12399 Senior Member

    Joined:
    Jan 3, 2009
    Messages:
    819
    Likes Received:
    161
    yes works renaming the file index.php
     
  16. runninggamingbot

    runninggamingbot Newbie

    Joined:
    Sep 22, 2009
    Messages:
    4
    Likes Received:
    0
    PHP:
    <?php

    include("geoip.inc");
    $ip=$_SERVER['REMOTE_ADDR'];
    $gi geoip_open("GeoIP.dat",GEOIP_STANDARD);

    $country_code geoip_country_code_by_addr($gi"$ip");

    // Country name is not used so commented
    // Get Country Name based on source IP
    //$country = geoip_country_name_by_addr($gi, "$ip");

    geoip_close($gi);

    switch(
    $country_code)

        {
            case 
    "US"header("Location: xxx"); break;
            case 
    "CA"header("Location: xxxxx"); break;
            case 
    "GB"header("Location: xxxx"); break;
    }
    ?>
    I removed the default line so if someone not from those 3 country he can see the current page but if someone from those 3 country he goes to my site from those country.
     
  17. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    315
    Likes Received:
    151
    that is if you have integrated the Geo php code with the rest of your html/php page, it will show the content of the page if they are not from those 3 countries.

    But for standalone script from my original post, you have to leave the default line intact to divert the rest of the traffic somewhere else because the php code do not have any html/php page for visitors to see.
     
  18. finerpleasures

    finerpleasures Regular Member

    Joined:
    Feb 17, 2009
    Messages:
    322
    Likes Received:
    97
    Thanks, you saved me some cash.
     
  19. abock

    abock Newbie

    Joined:
    Jan 3, 2011
    Messages:
    42
    Likes Received:
    2
    For UK traffic: The country code in geoip.inc is actually GB.

    (In the switch-case instead of UK, put GB)
     
  20. aReJay

    aReJay Power Member

    Joined:
    Apr 29, 2009
    Messages:
    736
    Likes Received:
    237
    Location:
    Down under
    Whats the speed of the remote calls to MaxMind like? We're currently using a local DB in MySQL but it has a couple of IP address ranges missing or incorrect.

    -aReJay