How to delete/unset a cookie on php?

后端 未结 6 1499
轻奢々
轻奢々 2021-01-17 11:38

I want to unset/delete my existing cookie with this:

setcookie (\"user\", \"\", time()-1); 
unset($user);

But cookies can not be deleted or

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 12:14

    Nothing - that code looks fine to me.

    Quoting the docs:

    When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser.

    setcookie ("TestCookie", "", time() - 3600);
    

    You may like to specify a time that's more in the past to avoid problems with the computer's time that may be a bit off.

    Additionally, in some cases it's useful to actually unset $_COOKIE['TestCookie'] as well.

提交回复
热议问题