Php 随机页面 url 自动重新加载
Php random page url automatic reload
我正在尝试让下面的 php 脚本自动将页面重新加载到包含网站页面的文件夹中的随机页面。我的代码只显示文本 link 或什么都不显示,并且不会自动重新加载到随机页面。这是代码:
<?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="answers">
<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>
请帮我修改代码!
此外,如果 link 用数字标记,是否有更简单的方法将这些页面添加到数组中。意思是我可以用一行代码做到这一点吗?
删除回显行并将其替换为:
header("Location: ".$urls[$random]);
exit;
如果您知道所有页面都是数字,您可以这样做:
$url = rand(1,24).'.html';
header("Location: ".$url);
exit;
这应该可以解决问题(如果您在页眉功能之前没有在网站上打印任何内容)
header("Location: /"rand(1,24).".html");
exit;
我正在尝试让下面的 php 脚本自动将页面重新加载到包含网站页面的文件夹中的随机页面。我的代码只显示文本 link 或什么都不显示,并且不会自动重新加载到随机页面。这是代码:
<?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="answers">
<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>
请帮我修改代码!
此外,如果 link 用数字标记,是否有更简单的方法将这些页面添加到数组中。意思是我可以用一行代码做到这一点吗?
删除回显行并将其替换为:
header("Location: ".$urls[$random]);
exit;
如果您知道所有页面都是数字,您可以这样做:
$url = rand(1,24).'.html';
header("Location: ".$url);
exit;
这应该可以解决问题(如果您在页眉功能之前没有在网站上打印任何内容)
header("Location: /"rand(1,24).".html");
exit;