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;
<?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>
Give this a try:
$tmp = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
$visited = array('b', 'f', 'h'); // get from session
if(count($tmp) == count($visited))
{
$visited = array();
// reset session
}
while(true)
{
$item = $tmp[rand(0, count($tmp) - 1)];
if(!is_array($item, $visited))
break;
}
$visited[] = $item;
// set session
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.
<?php
$all_page_links = ('a.php','b.php','c.php','d.php',
'e.php','f.php');
$visited_page_links = $_SESSION['visited_page_links'];
//eg data: 'c.php','f.php'
$can_visit_pages = array_diff($all_page_links,$visited_page_links);
if(!isset($_GET['next_page']) //this is the first visit
{
$can_visit_index = rand(0, count($can_visit_pages) - 1);
$current_page = $can_visit_pages[$can_visit_index];
unset($can_visit_pages[$can_visit_index]);
$visited_page_links[] = $current_page;
$next_page = $can_visit_pages[rand(0, count($can_visit_pages) - 1)];
include($current_page); //include the contents of the current
//page (or redirect). $current_page code will
//have the link to the $next_page.
}
else
if(isset($_GET['next_page']))
{
//TO-Do - pre-check if the page has been visited.
$current_page = $_GET['next_page'];
$visited_page_links[] = $current_page;
$current_page_index = array_search($current_page,$can_visit_pages);
unset($can_visit_pages[$current_page_index]);
$next_page = $can_visit_pages[rand(0, count($can_visit_pages) - 1)];
include($current_page); //include the contents of the current
//page (or redirect). $current_page code will
//have the link to the $next_page.
}
$_SESSION['visited_page_links'] = $visited_page_links;
?>