0

ページを繰り返さずにランダムに 8 つのページを表示するサイトを作成しようとしています。 filmpje3.php' など、8 ページすべてにアクセスするまで続きます。

いくつかのサイトを見回しましたが、見つけたコードは機能していないようです。リピートし続けました…

私が試したコードの例:

$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;
4

3 に答える 3

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>
于 2012-05-21T08:39:51.480 に答える
0

あなたが書いたコードを校正する代わりに、ここに新しいコードがあります。

コードは適切にテストして最適化できます/最適化する必要がありますが、うまくいきます。

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

?>

于 2012-05-21T09:20:59.667 に答える
0

これを試してください:

$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
于 2012-05-21T08:31:23.823 に答える