PHP - geoIP country redirect

Discussion in 'PHP' started by webdevel, Feb 20, 2011.

  1. webdevel

    webdevel New Member

    Joined:
    Feb 20, 2011
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    I could really use some help with a problem I'm having. I've read as many posts as i can find on this topic but unfortunatly I still cant get it to work.

    I would like to redirect visitors to different sites depending on their country. I just can't get this to work. Any help would be very much appreciated. Here is what I've got:

    Code:
    <?php
    require_once('geoip.inc');
    $gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
     
    if ($country == 'ca') { 
    header('Location: example.ca');
    }
    if ($country == 'nz') { 
    header('Location: example.co.nz');
    }
    if ($country == 'uk') { 
    header('Location: example.co.uk');
    }
    if ($country == 'us') { 
    header('Location: example.info');
    }
    ?>
     
    Last edited by a moderator: Feb 20, 2011
  2. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    The code looks perfectly fine to me...
    Whats the error can you specify..
     
  3. webdevel

    webdevel New Member

    Joined:
    Feb 20, 2011
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    I figured out that I could fix my problem with the following:

    <?php
    require_once('geoip.inc');
    $gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    $my_countries = 'us';
    if (strtolower($country) == $my_countries) {
    header('Location: example.us');
    }
    $my_countriess = 'nz';
    if (strtolower($country) == $my_countriess) {
    header('Location: example.co.nz);
    }
    $my_countriesss = 'uk';
    if (strtolower($country) == $my_countriesss) {
    header('Location: example.co.uk');
    }
    $my_countriessss = 'ca';
    if (strtolower($country) == $my_countriessss) {
    header('Location: example.ca');
    }
    ?>

    Thanks anyway for your help :)
     
  4. etiergjt

    etiergjt New Member

    Joined:
    Mar 21, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://forums.moneymakingfocus.com
    hmm,where did you find that ip range file?can you send me a copy?
     
  5. webdevel

    webdevel New Member

    Joined:
    Feb 20, 2011
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Go to maxmind.com
     
    shabbir likes this.
  6. etiergjt

    etiergjt New Member

    Joined:
    Mar 21, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://forums.moneymakingfocus.com
    thanks for your reply,but it's expensive,any cheaper solution for this?
     
  7. webdevel

    webdevel New Member

    Joined:
    Feb 20, 2011
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    It's free. What you want to download is GeoLite Country. It's 99.5% accurate
     
  8. etiergjt

    etiergjt New Member

    Joined:
    Mar 21, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://forums.moneymakingfocus.com
    oh,I see
    I didn't notice the free/opensource link
    Thanks
     
  9. pidge1992

    pidge1992 New Member

    Joined:
    Aug 2, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    but that code is not working for me :(

    anyone can help ?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice