Reputation: 3
I have a page that redirects to a random URL from a array of URLs.
<?php
$urls = array("url01",
"url02",
"url03");
$url = $urls[array_rand($urls)];
header("Location: http://$url");
?>
This is cool, but what I would REALLY like is each time the page is visited, instead of randomly choosing a URL to redirect to, I would like it to redirect sequentially on each visit?
Is this even possible?
Upvotes: 0
Views: 188
Reputation: 2512
<?php
session_start();
$curr = array_key_exists('curr', $_SESSION) ? $_SESSION['curr'] : 0;
$urls = array("url01", "url02", "url03");
$curr = $curr >= sizeof($urls) ? 0 : $curr;
$_SESSION['curr'] = $curr + 1;
header('Location: http://' . $urls[$curr]);
?>
Upvotes: 1