【问题标题】:Remembering last Displayed Banner记住最后显示的横幅
【发布时间】:2012-02-07 18:46:24
【问题描述】:

每次用户刷新页面时,我都会尝试显示随机横幅。我面临的问题是我不希望再次显示最后一个横幅。我有没有其他方法可以在不使用 cookie 或数据库实现的情况下记住最后显示的一个?

Cookie 实现:

<?php

$randIndex = rand(1,6);
if(!isset($_COOKIE["lastDispalyed"])){
    setcookie("lastDispalyed",$randIndex,time()+60*60*24);
}
else{       
    while($_COOKIE["lastDispalyed"] == $randIndex){
        $randIndex = rand(1,6);
    } 
    setcookie("lastDispalyed",$randIndex,time()+60*60*24);
} ?>

&lt;img src="images/mainBanners/&lt;?php echo $randIndex; ?&gt;.JPG"/&gt;

【问题讨论】:

  • 您可以使用$_SESSIONS。你为什么不想让他们看到另一个广告,而且如果你只有六个,你可能很快就会用完广告

标签: php html random setcookie


【解决方案1】:

您可以使用会话变量:

<?php

    session_start(); // <-- start session before outputting any HTML

    $randIndex = rand(1,6);

    if (!isset($_SESSION["lastDisplayed"])) {

        $_SESSION["lastDisplayed"] = $randIndex;

    } else {    

        while ($_SESSION["lastDisplayed"] == $randIndex) {
            $randIndex = rand(1,6);
        }

        $_SESSION["lastDisplayed"] = $randIndex;
    }

?>

参考session_start()

【讨论】:

  • @DarrylHebbes 它说了什么?
  • 它显示一个超时消息,实际上是我的错误。如果 rand(1,1) 发生,则 while 循环旋转出来。所以不用担心,你的例子很好。
【解决方案2】:

为横幅的 id 或 url 设置会话变量:

$_SESSION['lastDisplayed'] = $id; //could be the id of the banner you are displaying, or the file's url, whatever you have access too

然后在你去显示一个新的横幅时测试它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多