Umf
Umf

Reputation: 3

php each visit redirects to next page in a sequence

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?

  1. First visit redirects to url01
  2. Next visit redirects to url02
  3. Then next visit redirects to url03
  4. Then next visit redirects to url01.co.uk again and so on...

Is this even possible?

Upvotes: 0

Views: 188

Answers (1)

Deep
Deep

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

Related Questions