php doesn't read the cookie


php doesn't read the cookie

在我的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存在,则不执行任何操作。

Gr .

这只在cookie已经设置的情况下设置,您可能想要:

!isset($_COOKIE['test'])

你的代码应该像这样:

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

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

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

希望这对你有帮助