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...
An example of the code I tried:
$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;
Instead of proof-reading the code you wrote, here is a new code.
The code can be/should be optimized tested properly, but it does the trick.
Give this a try: