有没有一种方法可以使这个随机URL重定向脚本不会重复同一个URL多次


Is there a way to make this random URL redirect script not repeat the same URL more than once?

所以我想在网页中添加一点乐趣或"随机性",点击时有一个选项卡,将用户重定向到随机URL。但是,如果用户被重定向到该URL,我希望它不会重复该URL。

假设用户1点击"随机"http://google.com/但后来,他回到我的页面,第二次点击"随机",我希望它能把他带到其他地方,而不是回到http://google.com/。如果他被重定向到http://google.com/如果他第三次回去,但我只是不希望它重复相同的URL,如果他们刚刚访问了那个URL。

按照我描述的方式,这在PHP中可行吗?还是比我想象的更复杂?

这就是我目前正在处理的问题:(我一点也不懂PHP,这就是我为什么要问的原因)。

<?php
$urls = array(
"www.google.com",
"www.youtube.com",
"www.facebook.com"
);
$url = $urls[array_rand($urls)];
header("Location: http://$url");
?>

您可以通过使用会话来做到这一点:

<?php
    session_start();
    function checkUrl($url, $urls){
        if(count($_SESSION['visited']) == count($urls)){
               $_SESSION['visited'] = Array();
        }
        if(in_array($url, $_SESSION['visited'])){
             $url = $urls[array_rand($urls)];
             checkUrl($url, $urls);
        }
        else{
             $_SESSION['visited'][] = $url;
             header("Location: http://$url");
        }
    }
    $urls = array(
        "www.google.com",
        "www.youtube.com",
        "www.facebook.com"
    );
    $url = $urls[array_rand($urls)];
    checkUrl($url, $urls);
?>

编辑:

访问所有地址时执行重置