Ravi Ravi -4 years ago 134
PHP Question

Navigate to different pages when link is clicked

I have below code for

Index.php
file. Whenever the user clicks on the link from HTML page, then it redirects to the
Index.php
page. This page redirects randomly to the three different pages, namely: "index_control.php" , "index_non_intrusive.php" , "index_intrusive.php". But, now, I want to do is like: When the user clicks the link first it should navigate to the first link from the array(index_control.php),and when second user clicks the link, then 2nd link from the array(index_non_intrusive.php) so forth. And when the fourth user clicks on the link it should again redirect to the 1st link from array(index_control.php). so sort of sequence navigation not random. It would be great if someone can give some hint or help to achieve this.

Index.php


<html>
<head>
<title>
abc
</title>
</head>

<?php
$links= array("http://abc/index_control.php","http://abc/index_non_intrusive.php","http://abc/index_intrusive.php");
$randomLink = $links[rand(0, count($links)-1)];
header("Location: {$randomLink}");
exit();
?>

<html>

Answer Source

As "Sessions are a simple way to store data for individual users against a unique session ID" (found in @Oliver's link) I believe that sessions are not the tool to evenly distribute your (different!) users onto different URLs. You will need a globally accessible "variable". One very simple method could be to refer to an integer value that is stored in a file and incremented after each use, like

<?php
  $f="filename_for_integer_value.txt"; // make sure you have write access!
  $links= array("http://abc/index_control.php",
                "http://abc/index_non_intrusive.php",
                "http://abc/index_intrusive.php");
  $n=file_get_contents($f);
  file_put_contents($f,($n+1)%count($links));
  header("Location: {$links[$n]}");
  exit();
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download