Hi,
I want to use PHP to randomly redirect visitors to various websites, such as site1.com, site2.com, site3.com, etc. Could someone please supply me with the code?
Thanks in advance.
<?php $urls = array("www.site1.com", "www.site2.com", "www.site3.com"); $url = $urls[array_rand($urls)]; header("Location: http://$url"); ?>
Nice clean way to do it!
Depending on the ammount of urls, if a very large ammount you could use a mySQL databse and the rand() sql function.
Buts thats only if you had alot.
Nice, thanks for the code!
How would I be able to make it so that, for example, visits will be redirected to site1.com 50% of the time, and to site2.com and site3.com 25% of the time?
Canadian wrote:Nice, thanks for the code! How would I be able to make it so that, for example, visits will be redirected to site1.com 50% of the time, and to site2.com and site3.com 25% of the time?
Well that wouldn't be random would it? Easiest way would be enter the sites into the array multiple times. The 50% site could be in twice and the 25% sites only once each.