【问题标题】:php doesn't read the cookiephp 不读取 cookie
【发布时间】:2014-08-14 08:08:16
【问题描述】:

在我的 php 文件中我有这个..

if ( isset($_COOKIE['test']) ){
    setcookie("test", "testesttest", time()+3600*24*80);
}

    ob_start();
    echo 'autotrackphpre';
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    header("Location: $url");
ob_flush();

当你调用 php 文件时这是一个重定向.. 如果你先调用它,它会设置 cookie..

但是第二次加载.. cookie 得到更新... 那是我不想要的。

如果 cookie 存在,则什么也不做。

【问题讨论】:

  • if (!isset()) 那么?

标签: php redirect cookies


【解决方案1】:

这仅在您可能想要设置的情况下设置 cookie:

!isset($_COOKIE['test'])

【讨论】:

  • @Hanky 谢谢,我总是忘记集合是不规则的。
【解决方案2】:

你的代码应该是这样的:

if (!isset($_COOKIE['test']) ){
    setcookie("test", "testesttest", time()+3600*24*80);
}

【讨论】:

    【解决方案3】:

    你所做的与它应该做的完全相反。如果已设置 cookie,则您正在设置 cookie。这将使 php 更新 cookie。如下使用此代码

    if ( !isset($_COOKIE['test']) ){
        setcookie("test", "testesttest", time()+3600*24*80);
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2013-11-10
      • 1970-01-01
      相关资源
      最近更新 更多