如何重定向到随机HTML页面而不重复



我想将用户从索引页面(第1页(重定向到30个不同的页面,这些页面是随机选择的,不会重复。到目前为止,我可以设法重定向到随机页面,但我会重复。在所有页面2-31被随机重定向后,我希望用户看到相同的页面32。我该怎么做?

我试图用链接操作的代码如下:

$myLinks = array("../page2.html", 
"../page3.html",
"../page4.html", "../page5.html");  //It goes on until  page 31
$randomRedirection = $myLinks[array_rand($myLinks)]; 
header("Location: $randomRedirection"); 

我会把它放在会话中。将可能性数组存储在用户的会话中,然后获取下一页的索引,从可能性数组中删除该索引,然后重定向。

<?php
session_start();
if(!isset($_SESSION["possibilities"]))
{
//New user
$_SESSION["possibilities"] = ["/page1.html", "/page2.html", ...];
}
else if(count($_SESSION["possibilities"]) == 0)
{
//User has viewed all pages
header("Location: page32.html");
}
$index = rand(0, count($_SESSION["possibilities"])-1);
//Array splice removes the given elements from the array and returns them.
$page = array_splice($_SESSION["possibilities"], $index, 1);
header("Location:{$page[0]}");
?>

最新更新