Php随机页面网址自动重新加载

Php random page url automatic reload

本文关键字:新加载 加载 随机 Php      更新时间:2023-09-26

我正在尝试让下面的这个 php 脚本自动在下面将页面快速重新加载到包含网站页面的文件夹中的随机页面。我拥有的代码只显示文本链接或什么都没有,并且不会自动重新加载到随机页面。这是代码:

<?php 
//set the urls 
$urls = array("1.html" ,"2.html" ,"3.html" ,"4.html" ,"5.html" ,"6.html" ,"7.html" ,"8.html" ,"9.html" 
    ,"10.html" ,"11.html" ,"12.html" ,"13.html" ,"14.html" ,"15.html" ,"16.html" ,"17.html" ,"18.html" ,
    "19.html" ,"20.html" ,"21.html" ,"22.html" ,"23.html" ,"24.html" ); 
//set the number in (rand()%3); for however many links there are 
        $random = (rand()%24); 
echo ("<a href = '"$urls[$random]"); 
?>

因此,如果我有一个用户在 3.html 上的页面,并且他们单击了一个按钮,我希望脚本运行并从该数组中加载另一个随机页面,并在 URL 框中立即将页面重新加载到该页面。

3.html看起来像这样,这是按钮自动页面加载JavaScript的代码:

<h2 id="correct">CORRECT</h2>
<h2 id="wrong">WRONG</h2>
<div id="answerswers">
    <button onclick="document.getElementById('wrong').style.visibility='visible';
                 window.location = 'random.php';" id="one">1986</button>
                <button onclick="document.getElementById('correct').style.visibility='visible'; 
                 window.location = 'random.php';" id="two">1913</button>
                <button onclick="document.getElementById('wrong').style.visibility='visible';
                 window.location = 'random.phprandom.php';" id="three">1723</button>
                <button onclick="document.getElementById('wrong').style.visibility='visible';
                 window.location = 'random.php';" id="four">1812</button>
</div>

请帮我修复代码!

另外,如果链接用数字标记,是否有更简单的方法将这些页面添加到数组中。意思是我可以用一行代码做到这一点吗?

删除回声线并将其替换为:

header("Location: ".$urls[$random]);
exit;

如果您知道所有页面都是数字,则可以执行以下操作:

$url = rand(1,24).'.html';
header("Location: ".$url);
exit;

这应该可以解决问题(如果您在标题函数之前没有在网站上打印任何内容)

header("Location: /"rand(1,24).".html"); 
exit;