php包含随机文件,在pageload上没有重复 [英] php include random file with no repeat on pageload

查看:130
本文介绍了php包含随机文件,在pageload上没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用mt_rand在每次加载页面时显示指定文件夹中的随机文件。

I am currently using mt_rand to display a random file from the specified folder each time the page is loaded.

经过大量搜索后,我认为我需要创建一个数组,然后将数组洗牌,但不知道如何解决这个问题。

After doing lots of searching i think i need to create an array and then shuffle the array, but not sure how to go about this.

我发现的大多数例子都使用数组然后回显结果我试图包含结果。

Most of the examples i have found use an array and then echo the results where as i'm trying to include the result.

<?php
$fict = glob("spelling/*.php");
$fictional = $fict[mt_rand(0, count($fict) -1)];
include ($fictional);
?>


推荐答案

您可以使用会话cookie来保持随机,非 - 重复文件列表。实际上,为了安全起见,会话cookie应该只将 indices 列表存储到一个文件数组中。

You can use session cookies to hold a random, non-repeating list of files. Actually, for security, the session cookie should only store a list of indices into an array of files.

例如,假设我们有以下数组中的文件列表:

For example, suppose we have the following file list in an array:

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中存储 列表。然后,当我们浏览这个随机的索引列表时,我们得到序列:

We can create an array of the indices, e.g. array(0,1,2,3), shuffle them to get something like array(3,2,0,1), and store that list in the cookie. Then, as we progress through this random list of indices, we get the sequence:

spelling/file4.txt
spelling/file3.txt
spelling/file1.txt
spelling/file2.txt

cookie还将当前位置存储在这个指数列表中,当它到达结束时,我们重新洗牌并重新开始。

The cookie also stores the current position in this list of indices and when it reaches the end, we reshuffle and start over.

我意识到这一切听起来有点令人困惑所以也许这个华丽的图表会有所帮助:

I realize all this may sound a bit confusing so maybe this gorgeous diagram will help:

…或者也许是一些代码:

… or maybe some code:

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

?>

这篇关于php包含随机文件,在pageload上没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆