Php随机页面url自动重装

时间:2015-02-02 23:07:05

标签: javascript php jquery html random

我想在下面自动将这个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="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>

请帮我修复代码!

此外,如果链接用数字标记,是否有更简单的方法在数组中添加这些页面。我可以用一行代码来实现这个意义吗?

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

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

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