我创建了一个php脚本来控制弹出窗口的时间。我只想弹出每60秒显示一次。该脚本在用户第一次访问页面时设置cookie,然后对于后续访问,脚本会检查cookie,并且仅在cookie过期时才激活弹出窗口。弹出窗口由变量$ _SESSION ['activate_popup']控制。
除了用户第一次访问页面时,脚本在所有情况下都按预期工作。 cookie是空的,因此它应该设置cookie并在条件1中激活弹出窗口。相反,它将条件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/。搜索源代码“开始弹出窗口”,您将看到cookie已在条件1中设置,并在您第一次访问该页面时显示条件2中的输出。
答案 0 :(得分: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 -->';
}
答案 1 :(得分: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)。