Вы можете использовать файлы cookie сеанса для хранения случайного неповторяющегося списка файлов. На самом деле, в целях безопасности файл cookie сеанса должен хранить только список индексов в виде массива файлов.
Например, предположим, что у нас есть следующий список файлов в массиве:
index file
----------------------------
0 spelling/file1.txt
1 spelling/file2.txt
2 spelling/file3.txt
3 spelling/file4.txt
Мы можем создать массив индексов, например, array(0,1,2,3)
, перемешать их, чтобы получить что-то вроде array(3,2,0,1)
, и сохранить этот список в файле cookie. Затем, по мере прохождения этого случайного списка индексов, мы получаем последовательность:
spelling/file4.txt
spelling/file3.txt
spelling/file1.txt
spelling/file2.txt
Файл cookie также сохраняет текущую позицию в этом списке индексов, и когда он достигает конца, мы перетасовываем и начинаем заново.
Я понимаю, что все это может показаться немного запутанным, поэтому, возможно, вам поможет эта великолепная диаграмма: ![Великолепная диаграмма](https://i.stack.imgur.com/tpcHi.png)
или, может быть, какой-то код:
<?php
$fictional = glob("spelling/*.php"); // list of files
$max_index = count($fictional) - 1;
$indices = range( 0, $max_index ); // list of indices into list of files
session_start();
if (!isset($_SESSION['indices']) || !isset($_SESSION['current'])) {
shuffle($indices);
$_SESSION['indices'] = serialize($indices);
$_SESSION['current'] = 0; // keep track of which index we're on
} else {
$_SESSION['current']++; // increment through the list of indices
// on each reload of the page
}
// Get the list of indices from the session cookie
$indices = unserialize($_SESSION['indices']);
// When we reach the end of the list of indices,
// reshuffle and start over.
if ($_SESSION['current'] > $max_index) {
shuffle($indices);
$_SESSION['indices'] = serialize($indices);
$_SESSION['current'] = 0;
}
// Get the current position in the list of indices
$current = $_SESSION['current'];
// Get the index into the list of files
$index = $indices[$current];
// include the pseudo-random, non-repeating file
include( $fictional[$index] );
?>
person
Herbert
schedule
27.07.2013