PHP runs twice on page load when redirecting from non-www to www

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.

0
php cookies if-statement
Oct 26 '12 at 19:39
source share
2 answers

try the following:

 $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 -->'; } 
0
Oct 26
source share

It turns out that the problem described above is caused by the fact that PHP executes twice, once when the user first visits the non-www version of the page, and then redirects to the www version again.

It seemed that the script was (impossible) executing both the if and else statements, but instead, what happened was that the cookie was not set on the first pass, and therefore it set the cookie (condition 1) and then on the second pass it read that The cookie has already been set and has not expired (condition 2). This explains why the script exited 1-3 seconds between when the cookie was set and when the output was created, as can be seen from my comment on the attempted solution provided by the Relentless user.

To prevent your php script from being executed twice, just wrap it in 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 requests from http://domain.com to http://www.domain.com . If you are redirected to non-www instead, remove the exclamation point from the condition ($ pos! == false).

0
Oct 27
source share



All Articles