Hello, I have a file at http://sponsored.mystatichost.com/ip_address.txt and its got ip address' 58.6.0.0/17 58.6.128.0/17 58.7.0.0/16 58.65.248.0/21 58.84.64.0/18 58.84.128.0/18 58.84.192.0/20 58.84.208.0/21 58.87.0.0/20 Code (markup): I want to use php to redirecs all ips in http://sponsored.mystatichost.com/ip_address.txt to http://au.sponsored.mystatichost.com/ Thanks, Chris
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.
<?php $file = file('ip_address.txt'); if (in_array($_SERVER['REMOTE_ADDR'], $file)) { header('Location: http://au.sponsored.mystatichost.com/'); } ?> PHP:
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!