I want to unset/delete my existing cookie with this:
setcookie ("user", "", time()-1);
unset($user);
But cookies can not be deleted or unset. So what is problem?
Cookies are always stored in the client. The path only sets restrictions to what remote pages can access said cookies. For example, if you set a cookie with the path "/foo/" then only pages in the directory "/foo/" and subdirectories of "/foo/" can read the cookie.
Example 1: You can create the cookies by writing setcookie() and entering the expiry date of the cookie. If you want to delete the cookie then set the cookie expiry date to the current time. If you want to display the cookie then you can echo the cookie by $_cookie['name'] and it will print the cookie details.
Introduction. The superglobal $_COOKIE stores variables passed to current script along with HTTP request in the form of cookies.
you can unset cookies this way only may -1 not work
try this
setcookie ("user", "", time() - 3600);
When deleting a cookie you should assure that the expiration date is in the past.
Delete example:
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With