0

我创建了一个 php 脚本来控制弹出窗口的时间。我只想每 60 秒弹出一次。该脚本在用户第一次访问该页面时设置一个 cookie,然后对于后续访问,该脚本会检查该 cookie,并且仅在 cookie 过期时才激活弹出窗口。弹出窗口由变量 $_SESSION['activate_popup'] 控制。

脚本在所有情况下都按预期工作,但用户第一次访问页面时除外。cookie 是空的,因此它应该在条件 1 中设置 cookie 并激活弹出窗口。相反,它在条件 1 中设置 cookie 并在条件 2 中显示输出。

$GLOBALS['popup_output'] .= '<!-- begin popup -->';
$domain = 'brocktonvilla.com';
$expiration = time() + 60;
$time_until_expires = $_COOKIE['rc_popuup2'] - time();
$GLOBALS['popup_output'] .= '<!-- time until expires: ' . $time_until_expires . ' sec -->';

/* 1 */     if ( empty($_COOKIE['rc_popuup2']) ) {                                      // if cookie has not been set
                setcookie('rc_popuup2', $expiration, $expiration, '/', $domain );       // set cookie with value of cookie equals expiration time
                $_SESSION['activate_popup'] = 'yes';                                    // activate the popup
                $GLOBALS['popup_output'] .= '<!-- cookie empty => show popup & set cookie -->';             
            }       
/* 2 */     elseif ( $_COOKIE['rc_popuup2'] > time() ) {                                // cookie has been set and cookie expiration is greater than current time
                $_SESSION['activate_popup'] = 'no';                                     // do not activate popup
                $GLOBALS['popup_output'] .= '<!-- cookie set and not expired => do not show popup -->';
            }
/* 3 */     elseif ( $_COOKIE['rc_popuup2'] < time() ) {                                // cookie has been set and cookie expiration is less than current time
                $_SESSION['activate_popup'] = 'yes';                                    // activate the popup
                setcookie('rc_popuup2', $expiration, $expiration, '/', $domain );       // reset cookie with value of cookie equals expiration time
                $GLOBALS['popup_output'] .= '<!-- cookie set but has expired => show popup & reset cookie -->';
            }

您可以在此处查看运行中的脚本http://www.brocktonvilla.com/。搜索源代码“begin popup”,你会看到cookie已经被设置为条件1,并在你第一次访问页面时显示条件2的输出。

4

2 回答 2

0

事实证明,上述问题是由 PHP 执行两次引起的,一次是用户第一次访问非 www 版本的页面,然后是重定向到 www 版本。

似乎脚本(不可能)同时执行了 if 和 else 语句,但实际上发生的是在第一次传递时未设置 cookie,因此它设置了 cookie(条件 1),然后在第二次传递它读到 cookie 已经设置并且没有过期(条件 2)。这解释了为什么脚本输出在设置 cookie 和生成输出之间有 1-3 秒的时间,正如我对用户 Relentless 提供的尝试解决方案的评论中所见。

为了防止您的 php 脚本运行两次,只需将其包装为以下内容:

$domain = $_SERVER['SERVER_NAME'];                                                      
$needle = 'www.';
$pos = strpos($domain, $needle);
if( $pos !== false ) { 
        // your script here     
}       

此脚本假定您将对http://domain.com的所有服务器查询重定向到http://www.domain.com。如果您改为重定向到非 www,则从条件中删除感叹号($pos !== false)。

于 2012-10-27T21:31:49.220 回答
0

尝试这个:

$completed = false;
/* 1 */     if ( empty($_COOKIE['rc_popuup2']) ) {                                      // if cookie has not been set
                setcookie('rc_popuup2', $expiration, $expiration, '/', $domain );       // set cookie with value of cookie equals expiration time
                $_SESSION['activate_popup'] = 'yes';                                    // activate the popup
                $GLOBALS['popup_output'] .= '<!-- cookie empty => show popup & set cookie -->';             
            $completed = true;
            }       
/* 2 */     elseif (( $_COOKIE['rc_popuup2'] > time() ) && ($completed == false)) {                                // cookie has been set and cookie expiration is greater than current time
                $_SESSION['activate_popup'] = 'no';                                     // do not activate popup
                $GLOBALS['popup_output'] .= '<!-- cookie set and not expired => do not show popup -->';
             $completed = true;
            }
/* 3 */     elseif (( $_COOKIE['rc_popuup2'] < time() )  && ($completed == false)) {                                // cookie has been set and cookie expiration is less than current time
                $_SESSION['activate_popup'] = 'yes';                                    // activate the popup
                setcookie('rc_popuup2', $expiration, $expiration, '/', $domain );       // reset cookie with value of cookie equals expiration time
                $GLOBALS['popup_output'] .= '<!-- cookie set but has expired => show popup & reset cookie -->';
            }
于 2012-10-26T20:04:47.160 回答