0

I've created a php script to control the timing of a popup window. I only want to popup to display once per 60 seconds. The script sets a cookie the first time the user visits the page, and then for subsequent visits the script checks the cookie and only activates the popup if the cookie has expired. The popup is controlled by the variable $_SESSION['activate_popup'].

The scripts works as intended in all cases except for the when the user visits the page for the first time. The cookie is empty and so it should set the cookie and activate the popup in condition 1. Instead, it sets the cookie in condition 1 and displays the output in condition 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/. Search the source code "begin popup" and you will see that the cookie has both been set in condition 1 and displays the output in condition 2 the first time you visit the page.

tbradley22
  • 1,535
  • 2
  • 15
  • 14
  • 2
    When you do "View source", most browsers will re-download the page, so it's actually showing the source from the second access. If you want to see the original source that was downloaded, use the browser's Developer Tools. – Barmar Oct 26 '12 at 20:24

2 Answers2

0

try this:

$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 -->';
            }
Barmar
  • 741,623
  • 53
  • 500
  • 612
Relentless
  • 122
  • 6
  • You should explain how `$completed` is intended to be used. – Barmar Oct 26 '12 at 20:19
  • this solution did not work. the output is identical the the originally submitted script. ie it is somehow setting the cookie and outputting that the cookie has been set at the same time. – tbradley22 Oct 26 '12 at 21:24
0

It turns out that the issue described above is being caused by PHP executing twice, once when the user first visits the non-www version of the page and then again on the redirect to the www version.

It seemed that the script was (impossibly) executing both an if and else statement, but instead what was happening is that on the first pass the cookie was not set and therefore it set the cookie (condition 1), and then on the second pass it read that the cookie had already been set and not expired (condition 2). This explains why the script outputs that there had been 1-3 seconds elapsed between when the cookie was set and when the output was generated, as seen in my comment to the attempted solution provided by user Relentless.

To prevent your php script from running twice, just wrap it with the following:

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

This script assumes that you redirect all server queries for http://domain.com to http://www.domain.com. If you are instead redirecting to the non-www then remove the exclamation point from the condition ($pos !== false).

tbradley22
  • 1,535
  • 2
  • 15
  • 14