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:
    341
    Likes Received:
    158
    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 44
  2. DarkPixel

    DarkPixel Jr. VIP Jr. VIP

    Joined:
    Oct 4, 2011
    Messages:
    1,349
    Likes Received:
    1,257
    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!
     
  3. manolo12399

    manolo12399 Senior Member

    Joined:
    Jan 3, 2009
    Messages:
    1,178
    Likes Received:
    204
    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
     
  4. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    341
    Likes Received:
    158
    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
  5. jairathnem

    jairathnem Power Member

    Joined:
    Oct 27, 2010
    Messages:
    550
    Likes Received:
    316
    How do i add it to wordpress?
     
  6. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    341
    Likes Received:
    158
    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.
     
  7. manolo12399

    manolo12399 Senior Member

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


    UPDATE: work perfect.

    Thanks.
     
    Last edited: Nov 22, 2011
  8. 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
     
  9. Essence19

    Essence19 BANNED BANNED

    Joined:
    Aug 10, 2010
    Messages:
    830
    Likes Received:
    353
    I was looking for it the whole of yest and now i see you posted it here.lol thanks and rep.it works great.
     
  10. 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.
     
  11. runninggamingbot

    runninggamingbot Newbie

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

    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
     
    Last edited by a moderator: Oct 30, 2018
  12. NilssonCPA

    NilssonCPA Junior Member

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

    manolo12399 Senior Member

    Joined:
    Jan 3, 2009
    Messages:
    1,178
    Likes Received:
    204
    yes works renaming the file index.php
     
  14. runninggamingbot

    runninggamingbot Newbie

    Joined:
    Sep 22, 2009
    Messages:
    3
    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.
     
  15. mointernet

    mointernet Regular Member

    Joined:
    Apr 21, 2008
    Messages:
    341
    Likes Received:
    158
    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.
     
  16. 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)
     
  17. aReJay

    aReJay Power Member

    Joined:
    Apr 29, 2009
    Messages:
    735
    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
     
  18. daStick

    daStick Newbie

    Joined:
    Oct 28, 2010
    Messages:
    47
    Likes Received:
    15
    uber bump. This has saved me tons. Thanks!
     
  19. realidan

    realidan Newbie

    Joined:
    Feb 28, 2009
    Messages:
    16
    Likes Received:
    5
    Let's say you wanted to put the .php script on every page of your site and then send all visitors from... say China...to ****** or any other URL.

    Would we need to place the .inc and .dat file in a different location or will things still work as you have them laid out?
     
  20. pasenseoso

    pasenseoso Power Member

    Joined:
    Aug 19, 2011
    Messages:
    761
    Likes Received:
    138
    Occupation:
    hachetman
    Location:
    - - P I L I P I N A S - -
    Home Page:
    @realidan dude, i think you can directly use your ****** link in the single php file. just specify which location will you want your link to go live.

    @OP

    You just made my day! :) This is the best script sooooooooooooooooo far! Thanks a lot!