我以前曾使用过会议,但从未饼干。我想使用cookie的原因有两个:
1)学习是新的东西
2)我想在一个小时左右的时间内让cookie到期(我知道在代码示例中它在40秒内到期)

我正在尝试写一个基本的if语句

      if($counter=="1") { //do this second 
} 
        elseif ($counter >="2") { //do this every time after the first and second
} 
        else {// this is the first action as counter is zero
}

这是我用来设置cookie的代码:

 // if cookie doesnt exsist, set the default
    if(!isset($_COOKIE["counter_cookie"])) {
        $counter = setcookie("counter_cookie", 0 ,time()+40);

    }

    // increment it
     $counter++;



    // save it
     setcookie("counter_cookie", $counter,time()+40);
     $counter = $_COOKIE["counter_cookie"];

问题是计数器将设置为0到1,但不会设置为1到2,依此类推。任何帮助都很棒,我知道这是一个非常简单的愚蠢问题:|

谢谢!

有帮助吗?

解决方案

问题很可能与这一行有关:

$counter = setcookie("counter_cookie", 0 ,time()+40);

看来您期望SetCookie返回一个值,但这不会发生。取而代之的是,SetCookie将简单地归还成功的布尔值,而在失败方面的错误。

http://php.net/manual/en/function.setcookie.php

您可以尝试这样的重写以实现预期的效果:

if(isset($_COOKIE["counter_cookie"]))
{
  $counter = $_COOKIE["counter_cookie"];
}
else
{
  $counter = 0;
}
$counter++
setcookie("counter_cookie", $counter ,time()+40);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top