【问题标题】:PHP executes twice on page load when redirecting from non-www to www从非 www 重定向到 www 时,PHP 在页面加载时执行两次
【发布时间】:2012-10-28 05:58:38
【问题描述】:

我创建了一个 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']) ) {                                      // 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 -->';
            }

您可以在此处http://www.brocktonvilla.com/ 看到正在运行的脚本。搜索源代码“begin popup”,你会看到cookie已经被设置为条件1,并且在你第一次访问页面时显示条件2的输出。

【问题讨论】:

  • 当你做“查看源代码”时,大多数浏览器会重新下载页面,所以它实际上是显示第二次访问的源代码。如果您想查看下载的原始源代码,请使用浏览器的开发者工具。

标签: php cookies if-statement


【解决方案1】:

试试这个:

$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 的用途。
  • 此解决方案不起作用。输出与最初提交的脚本相同。即它以某种方式设置cookie并输出同时设置了cookie。
【解决方案2】:

事实证明,上述问题是由 PHP 执行两次引起的,一次是用户第一次访问非 www 版本的页面,然后又是重定向到 www 版本。

脚本似乎(不可能)同时执行了 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 ) { 
        // your script here     
}       

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2018-03-31
    • 2015-01-28
    相关资源
    最近更新 更多