【发布时间】:2023-03-16 05:46:01
【问题描述】:
我有一个数组$files,其中包含一个目录中的 php 文件列表。每次加载时,其中一个文件应随机包含在页面中。因此我改组了数组shuffle($files);。
为避免连续加载相同的 php 包含,我希望将洗牌后的数组存储在会话 cookie 中,因此每次刷新页面时都会通过数组进行循环。当到达数组的末尾时,应该生成一个新的打乱数组,......
我找到了this,但它对我不起作用。 这是我目前所拥有的:
PHP
// Get files from directory and store them in an array
$files = glob("teaser-images/*.php");
// Start session
session_start();
// Randomize array and store it in a session cookie
shuffle($files);
// If there’s already a cookie find the corresponding index and loop trough the array each refresh
if (isset($_SESSION['last_index'])) {
$_SESSION['last_index'] = …
// If the end of the array is reached shuffle it again and start all over
}
// If there’s no cookie start with the first value in the array
else {
$_SESSION['last_index'] = …
}
// Include the php file
include($random_file);
【问题讨论】:
-
您提供了另一个答案的链接,并说它不适合您。它是如何“不工作”的?错误是什么?
-
我没有发现任何错误,但没有显示任何内容……
标签: php session-cookies shuffle