PHP IP Redirect Help? Please

Discussion in 'PHP' started by ChristopherLeeMaher.Com, Aug 31, 2010.

  1. #1
    ChristopherLeeMaher.Com, Aug 31, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    As they are all ip address 58. something, we can assume 58 is Australia ? so if you wanted all Australian visitors to go to au.sponsored.... you could use this.


    
    <?php
    $visitor = $_SERVER['REMOTE_ADDR'];
    if (preg_match("#58.\d{0,3}.\d{0,3}.\d{0,3}#",$visitor)) {
          header('Location: http://au.sponsored.mystatichost.com/');
    } else {
          header('Location: http://sponsored.mystatichost.com');
    };
    ?> 
    
    PHP:
    However its limited to ip 58. and as I'm sure you are aware there's many many more Australian IP address. Something like this would probable suit your needs better http://www.ip2country.net/geo-targeting/target-country-faq.html but the above is an easy solution for what you asked.
     
    Last edited: Aug 31, 2010
    MyVodaFone, Aug 31, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    
    $file = file('ip_address.txt');
    
    if (in_array($_SERVER['REMOTE_ADDR'], $file)) {
          header('Location: http://au.sponsored.mystatichost.com/');
    }
    
    ?>
    PHP:
     
    danx10, Aug 31, 2010 IP
  4. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Yes, the above solutions would work ... I would however probably add an "echo" command as well with the usual message "If this page doesn't redirect, click here ....". Do remember that this echo command will need to go before the header command and you will have to enclose both these commands between ob_start() and ob_flush(). Hope this helps!
     
    dfsweb, Aug 31, 2010 IP