PHP随机链接无需重复 [英] PHP random links without repeating
本文介绍了PHP随机链接无需重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图建立一个网站,以随机方式引导您浏览8页,而不重复页面,例如:
首先'filmpje4.php',其中包含指向'filmpje8的链接。 php',导致'filmpje3.php',等等,直到所有八个页面都被访问过。
I'm trying to put up a site which leads you through eight pages in a random way, without repeating the pages, for example: First 'filmpje4.php', which contains a link to 'filmpje8.php', which leads to 'filmpje3.php', etcetera, until all eight pages have been visited.
我在几个网站上环顾四周,但我找到的代码似乎没有工作。我一直在重复...
I have looked around on several sites, but the codes I found didn't appear to work. I kept getting repeats...
我尝试过的代码示例:
$links = array('<a href="filmpje1.php">filmpje1</a>', [...]'<a href="filmpje8.php">filmpje8</a>');
// get users visited links to an array
$visited_links = explode('|', $_SESSION['visited_links']);
// remove visited links from links array
foreach($visited_links as $visited_link) {
unset($links[array_search($visited_link, $links)]);
}
// get a random link from unvisited links
$link = $links[rand(0, count($links)-1)];
// add the selected link to visited array
$visited_links[] = $link;
// save visited links to user session as | separated string
$_SESSION['visited_links'] = implode('|', $visited_links);
echo $link;
推荐答案
<?php
session_start();
if (!isset($_SESSION["visited_pages"])) { $_SESSION["visited_pages"] = array(); }
$links = array("file1.php","file2.php","file3.php","file4.php");
$nonvisited_links = array_values(array_diff($links,$_SESSION["visited_pages"]));
$next_index = mt_rand(0,sizeof($nonvisited_links)-1);
$_SESSION["visited_pages"][] = $next_page = $nonvisited_links[$next_index];
?>
<a href="localhost/<?php echo $next_page; ?>">visit!!</a>
这篇关于PHP随机链接无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文