当从非www重定向到www时,PHP在页面加载时执行两次 [英] PHP executes twice on page load when redirecting from non-www to www

查看:118
本文介绍了当从非www重定向到www时,PHP在页面加载时执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个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'])){//如果没有设置cookie
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 =>显示弹出和set cookie - >';
}
/ * 2 * / elseif($ _COOKIE ['rc_popuup2']> time()){// cookie已设置,Cookie到期时间大于当前时间
$ _SESSION ['activate_popup'] ='no'; // do not activate popup
$ GLOBALS ['popup_output']。='<! - cookie set and not expired =>不显示弹出式窗口 - >';
}
/ * 3 * / elseif($ _COOKIE ['rc_popuup2']< time()){// cookie已设置,Cookie到期时间小于当前时间
$ _SESSION ['activate_popup'] ='yes'; //激活弹出框
setcookie('rc_popuup2',$ expiration,$ expiration,'/',$ domain); // reset cookie with value of cookie equals expiration time
$ GLOBALS ['popup_output']。='<! - cookie set but has expired =>显示弹出和重置Cookie - >';
}

您可以在 http://www.brocktonvilla.com/ 。搜索源代码begin popup,您将看到该cookie已在条件1中设置,并在您第一次访问该页面时显示条件2中的输出。



似乎该脚本(不可能)执行了一个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){
//您的脚本在这里
}

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


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.

解决方案

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).

这篇关于当从非www重定向到www时,PHP在页面加载时执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆