我有一個數組$files
包含一個目錄中的php文件列表。每次加載時,其中一個文件應隨機包含在頁面中。因此,我洗了陣列shuffle($files);
。在會話cookie中存儲隨機數組並通過它循環
爲了避免同一個php include被加載到一行中,我希望將shuffle數組存儲在會話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);
您提供了另一個答案的鏈接,並表示它不適用於您。它如何「不起作用」?什麼是錯誤? – naomik
我發現沒有錯誤,但沒有顯示任何內容... – user1706680