Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中单击按钮即可生成随机(非重复)文件生成器_Php_Random_Webpage - Fatal编程技术网

在php中单击按钮即可生成随机(非重复)文件生成器

在php中单击按钮即可生成随机(非重复)文件生成器,php,random,webpage,Php,Random,Webpage,我想随机显示5个php文件的内容,但在使用php/javascript(在php桌面上也可以使用)单击按钮[下一步]时,不重复 我使用的代码确实在页面加载时显示了随机网页,但我确实遇到了重复的网页 这是我用于index.php文件的代码: <?php $RandomList = array(); $RandomList[] = "/review/review-a1.php"; $RandomList[] = "/review/review-a2.php"; $RandomList[] =

我想随机显示5个php文件的内容,但在使用php/javascript(在php桌面上也可以使用)单击按钮[下一步]时,不重复

我使用的代码确实在页面加载时显示了随机网页,但我确实遇到了重复的网页

这是我用于index.php文件的代码:

<?php
$RandomList = array();
$RandomList[] = "/review/review-a1.php";
$RandomList[] = "/review/review-a2.php";
$RandomList[] = "/review/review-a3.php";
$RandomList[] = "/review/review-a4.php";
readfile($_SERVER['DOCUMENT_ROOT'].$RandomList[rand(0,count($RandomList)-1)]);
?>


请建议如何获取非重复文件。

只需保存已在中使用的路径即可:


$RandomList
中选择文件后,只要在随机重新选择文件时将其从该列表中删除即可。利用。如果你想在多个页面浏览时保持列表,你需要使用。请你详细说明一下,因为我对这些代码还不熟悉!感谢您的帮助,我已经添加了最后5页的链接-其他部分的最后一页。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。但我还是得到了访问页面的重复次数!!!还有一件事,现在我刚刚更改了
$randomList=[“/review/review-a1.php”、“/review/review-a2.php”、“/review/review-a3.php”、“/review/review-a4.php”]
$randomList=[“review1.php”、“review2.php”、“review3.php”、“review4.php”]尝试了您编辑的版本。但是仍然得到-->警告:readfile**([此处文件的本地url]):**无法打开流:第23行的[此处文件的本地url]中没有此类文件或目录在该服务器上找不到请求的url/rnd-norep.php。在wamp上使用时显示。。。。。。。。。。。。。。。。。。。。。。。上面讨论的代码是我在php桌面上运行的代码enviroment@user9616292嘿“警告:readfile()无法打开流。”表示您试图打开的路径不存在。该文件不存在或存储在其他文件夹中。请尝试
var\u dump([my local url of the file here])
(debug),以确定所需的路径是否实际是存储文件的路径。第二个问题是等价的。遗憾的是,我怀疑我们是否可以帮助您解决这些问题,但它们通常可以通过调试轻松解决
//Read visited paths from session or create a new list. 
//?? works only in PHP7+. Use isset()?: instead of ?? for previous versions
$visitedPaths = $_SESSION['visitedPaths'] ?? [];

//Your list, just slightly changed syntax. Same thing
$randomList = [
    "/review/review-a1.php",
    "/review/review-a2.php",
    "/review/review-a3.php",
    "/review/review-a4.php"
];

//Remove all paths that were already visited from the randomList
$randomList = array_diff($randomList, $visitedPaths);

//You need to check now if there are paths left
if (!empty($randomList)) {
    //The user did not load all files, so we can show him the next one
    //Use array_rand() rather than $array[rand(0, count($array) -1)]
    $randomPath = $randomList[array_rand($randomList)];
    readfile($_SERVER['DOCUMENT_ROOT'] . $randomPath);

    //Now we need to save, that the user loaded this file
    $visitedPaths[] = $randomPath;

    //And we need to save the new list in the session
    $_SESSION['visitedPaths'] = $visitedPaths;
} else {
    //TODO: Add some logic in case all paths have been visited
}