I created a php script to control the popup window time. I want the popup to be displayed only once every 60 seconds. The script sets a cookie the first time a user visits the page, and then for subsequent visits, the script checks the cookie and activates the pop-up only if the cookie has expired. The popup is controlled by the variable $ _SESSION ['activate_popup'].
Scripts work as intended in all cases, except when the user first visits the page. The cookie is empty, so it must set a cookie and activate the pop-up in state 1. Instead, it sets the cookie to state 1 and displays the result in state 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 -->'; }
You can see the script in action here http://www.brocktonvilla.com/ . Find the source code for "begin popup" and you will see that the cookie was set to state 1 and displays the result in state 2 the first time you visit the page.
php cookies if-statement
tbradley22 Oct 26 '12 at 19:39 2012-10-26 19:39
source share