How to delete / delete cookie on php?

I want to delete / delete an existing cookie with this:

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

But cookies cannot be deleted or canceled. So what is the problem?

+6
source share
6 answers

you can disable cookies in such a way that only -1 may not work

try it

 setcookie ("user", "", time() - 3600); 
+15
source

When deleting a cookie, you must ensure that the expiration date is in the past.

Uninstall example:

 // set the expiration date to one hour ago setcookie("user", "", time()-3600); 
+4
source

Nothing - this code looks good to me.

Quoting documents:

When deleting a cookie, you must make sure that the expiration date is in the past in order to start the removal mechanism in your browser.

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

You may need to specify a time that is longer in the past in order to avoid problems with computer time, which may be slightly off.

In addition, in some cases, it is also useful to disable $_COOKIE['TestCookie'] .

+2
source

// SHOULD provide the root path or any specific cookie path

 //SET COOKIE setcookie ("user", "", time() + 3600 , '/'); //UNSET COOKIE setcookie ("user", "", time()-100 , '/' ); // past time 
+2
source

As already mentioned, when deleting a cookie you must make sure that the expiration date is in the past.

BUT you should also use the same path and even the delete domain that you used to create cookies, so if you create cookies like this

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

to delete this cookie use this code

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

and also it is better to use a certain date in the past instead of time () - 3600

+1
source
 setcookie ("user", "", time() - 3600); //will reset cookie(client,browser) unset($_COOKIE["user"]); // will destroy cookie(server) 
+1
source

Source: https://habr.com/ru/post/902838/


All Articles