Remove a cookie

后端 未结 22 1245
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 16:16

When I want to remove a Cookie I try

unset($_COOKIE[\'hello\']);

I see in my cookie browser from firefox that the cookie still exists. How

22条回答
  •  隐瞒了意图╮
    2020-11-22 16:41

    I had the same problem in my code and found that it was a cookie path issue. Check out this stack overflow post: Can't delete php set cookie

    I had set the cookie using a path value of "/", but didn't have any path value when I tried to clear it, so it didn't clear. So here is an example of what worked:

    Setting the cookie:

    $cookiePath = "/";
    $cookieExpire = time()+(60*60*24);//one day -> seconds*minutes*hours
    setcookie("CookieName",$cookieValue,$cookieExpire,$cookiePath);
    

    Clearing the cookie:

    setcookie("cookieName","", time()-3600, $cookiePath);
    unset ($_COOKIE['cookieName']);
    

    Hope that helps.

提交回复
热议问题