PHP random links without repeating

那年仲夏 提交于 2019-12-25 05:16:05

问题


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;

回答1:


<?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>



回答2:


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



回答3:


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;

?>



来源:https://stackoverflow.com/questions/10681786/php-random-links-without-repeating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!