How to delete/unset a cookie on php?

后端 未结 6 1498
轻奢々
轻奢々 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:29

    As already was said - when deleting a cookie you should assure that the expiration date is in the past.

    BUT you also have to use the same path and even domain for deleting, which you used for cookie creating, so if create cookie like this

    setcookie ("user", "John", time()+7200, '/', 'mydomain.com'); 
    

    to delete this cookie use this code

    setcookie ("user", "", time()-3600, '/', 'mydomain.com');
    

    and also better use specific date in the past instead of time() - 3600

提交回复
热议问题