Web Hosting Talk







View Full Version : PHP Index randomizer script?


net41
05-08-2005, 03:00 PM
I am looking for a simple php header redirect, that will goto 2 different sites randomly.

So if I had blah.com, and wanted it to take half the users to one site, and half the users to another site. How would I go about doing this?

:confused:

TehBooster
05-08-2005, 03:30 PM
<?php
$websites = array (
0 => 'http://www.google.com/',
1 => 'http://www.ebay.com/',
2 => 'http://news.bbc.co.uk/'
);

$websites_keys = array_keys( $websites );
$websites_keys_max = max( $websites_keys );
$websites_keys_min = min( $websites_keys );

srand();
header('location:' . $websites[ rand( $websites_keys_min, $websites_keys_max ) ]);
exit;
?>

You might try something like this, while its not exact, it will direct to any number of websites randomly.

error404
05-08-2005, 08:12 PM
Or much simpler:

$websites = array ('http://www.google.com/', 'http://www.ebay.com/', 'http://news.bbc.co.uk/',);

srand();
header("Location: ".$websites[rand(0, count($websites) - 1)]);

raulgonzalez
05-08-2005, 08:55 PM
Insert some URLs into a database, that will be easier to maintain.

$query = "SELECT * FROM Random_Table order by rand() limit 1";

if ($r = mysql_query ($query)) {


while ($row = mysql_fetch_array ($r)) {

header("Location: {$row['url']}");

}


Just an idea!