Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当从非www重定向到www时,PHP在页面加载时执行两次_Php_Cookies_If Statement - Fatal编程技术网

当从非www重定向到www时,PHP在页面加载时执行两次

当从非www重定向到www时,PHP在页面加载时执行两次,php,cookies,if-statement,Php,Cookies,If Statement,我创建了一个php脚本来控制弹出窗口的计时。我只想弹出显示每60秒一次。该脚本在用户第一次访问页面时设置cookie,然后对于后续访问,该脚本将检查cookie并仅在cookie过期时激活弹出窗口。弹出窗口由变量$\u SESSION['activate\u popup']控制 除了用户第一次访问页面时,脚本在所有情况下都按预期工作。cookie为空,因此应设置cookie并在条件1中激活弹出窗口。相反,它在条件1中设置cookie,并在条件2中显示输出 $GLOBALS['popup_outp

我创建了一个php脚本来控制弹出窗口的计时。我只想弹出显示每60秒一次。该脚本在用户第一次访问页面时设置cookie,然后对于后续访问,该脚本将检查cookie并仅在cookie过期时激活弹出窗口。弹出窗口由变量$\u SESSION['activate\u 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 -->';
            }
$GLOBALS['popup\u output']。='';
$domain='brocktonvilla.com';
$expiration=time()+60;
$time_until_expires=$_COOKIE['rc_popuup2']-time();
$GLOBALS['popup_output'].='';
/*1*/if(空($\u COOKIE['rc\u popuup2']){//if尚未设置COOKIE
setcookie('rc_popuup2',$expiration,$expiration,'/',$domain);//设置cookie,cookie值等于过期时间
$\u会话['activate\u popup']=“yes”;//激活弹出窗口
$GLOBALS['popup_output'].='';
}       
/*2*/elseif($\u COOKIE['rc\u popuup2']>time()){//COOKIE已设置且COOKIE过期时间大于当前时间
$\u会话['activate\u popup']=“no”;//不激活popup
$GLOBALS['popup_output'].='';
}
/*3*/elseif($\u COOKIE['rc\u popuup2']
您可以在这里看到正在运行的脚本。搜索源代码“begin popup”(开始弹出窗口),您将看到cookie已在条件1中设置,并在您第一次访问页面时在条件2中显示输出。

尝试以下操作:

$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 -->';
            }
$completed=false;
/*1*/if(空($\u COOKIE['rc\u popuup2']){//if尚未设置COOKIE
setcookie('rc_popuup2',$expiration,$expiration,'/',$domain);//设置cookie,cookie值等于过期时间
$\u会话['activate\u popup']=“yes”;//激活弹出窗口
$GLOBALS['popup_output'].='';
$completed=true;
}       
/*2*/elseif($_COOKIE['rc_popuup2']>time())&($completed==false)){//COOKIE已设置且COOKIE过期时间大于当前时间
$\u会话['activate\u popup']=“no”;//不激活popup
$GLOBALS['popup_output'].='';
$completed=true;
}
/*3*/elseif($\u COOKIE['rc\u popuup2']
事实证明,上述问题是由PHP执行两次引起的,一次是在用户首次访问非www版本的页面时,另一次是在重定向到www版本时

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

要防止php脚本运行两次,只需使用以下内容将其包装:

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

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

您应该解释如何使用
$completed
。此解决方案不起作用。输出与最初提交的脚本相同。即,它以某种方式设置cookie,并输出cookie已同时设置。当您执行“查看源代码”时,大多数浏览器都会重新下载页面,因此它实际上是从第二次访问中显示源代码。如果要查看下载的原始源代码,请使用浏览器的开发人员工具。