【问题标题】:isset($_COOKIE['cname']) not working on using php refresh headerisset($_COOKIE['cname']) 无法使用 php 刷新标头
【发布时间】:2013-07-17 00:18:10
【问题描述】:

在第一次访问我的网站时,我使用以下代码将用户重定向到欢迎页面。

// INDEX.PHP

include('backend-scripts/constants.php');

// set a cookie for the first website visit of the day.
if (isset($_COOKIE['firstVisit'])) {
    continue;
}
else {
    setcookie('firstVisit', 'no', time()+50, '/');

    header("Location: $baseURL/pages/welcome.php");
}

else 子句完美运行并重定向到welcome.php。但是在welcome.php中,当我想重定向回主页时,我执行以下操作:

// WELCOME.PHP

include('backend-scripts/constants.php');

header("Refresh: 5;url=$baseURL");

最后是 $baseURL:

// CONSTANTS.PHP

$serverName = $_SERVER['SERVER_NAME'];

$baseURL = "http://".$serverName.":13215";

但它并没有重定向回主页,welcome.php 不断刷新。

我的猜测:它可能会成功重定向到主页,但不知何故发现 $_COOKIE['firstVisit'] 未设置并返回到welcome.php

关于如何解决这个问题的任何想法?

【问题讨论】:

  • continue 在循环内吗?如果没有,那就没有意义了。
  • 嘿,请贴更多代码,$baseURL 下存储了什么?在发送标头刷新之前尝试回显它。命名文件,在哪里,因为这段代码看起来“很好”,但问题出在代码的其他部分。 + 是 删除 继续。
  • 尝试延长 cookie 的过期时间。
  • 我通常通过在if() 语句之前放置var_dump($_COOKIE) 来开始调试这种类型,这样您就可以确定cookie 的状态。同样正如 JM Verastigue 所说 - 您的 cookie 很快就会过期。我什至会尝试从现在起 5 小时后的到期时间,以确保
  • 50 秒仍然是 5 秒刷新时间的 10 倍。虽然最初的 cookie 打算持续 24 小时,但我缩短了开发阶段的时间。但为了确保让我重试更长的时间。

标签: php


【解决方案1】:

试试这个

 // WELCOME.PHP

 include('backend-scripts/constants.php');
 global $baseURL;
 header("Refresh: 5;url=$baseURL");

将 if 块留空也可以正常工作

 if (isset($_COOKIE['firstVisit']))
 {

 }

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 2018-08-31
    • 1970-01-01
    • 2012-12-15
    • 2010-12-25
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多