Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to delete cookies server-side

Tags:

http

cookies

For my authentication process I create a unique token when a user logs in and put that into a cookie which is used for authentication.

So I would send something like this from the server:

Set-Cookie: token=$2a$12$T94df7ArHkpkX7RGYndcq.fKU.oRlkVLOkCBNrMilaSWnTcWtCfJC; path=/; 

Which works on all browsers. Then to delete a cookie I send a similar cookie with the expires field set for January 1st 1970

Set-Cookie: token=$2a$12$T94df7ArHkpkX7RGYndcq.fKU.oRlkVLOkCBNrMilaSWnTcWtCfJC; path=/; expires=Thu, Jan 01 1970 00:00:00 UTC;  

And that works fine on Firefox but doesn't delete the cookie on IE or Safari.

So what is the best way to delete a cookie (without JavaScript preferably)? The set-the-expires-in-the-past method seems bulky. And also why does this work in FF but not in IE or Safari?

like image 968
Joshkunz Avatar asked Mar 12 '11 22:03

Joshkunz


People also ask

How are cookies stored server side?

Cookies are stored in the client's browser with a timeout after which they are deleted. Upon every HTTP request to the server, they are sent to the server automatically. The cookie is usually set by the server, not the client (but it's possible).

Are cookies server side or client side?

Cookies are client-side files that are stored on a local computer and contain user information. Sessions are server-side files that store user information. Cookies expire after the user specified lifetime. The session ends when the user closes the browser or logs out of the program.

Are cookies accessible on server side?

cookies are always client-side. Session cookies are stored on the client machine and at a minimum contain a reference to the session Id. If a server has a cookie it's because it's acting as a client. You can add cookies with JavaScript or from the server, that's probably what they mean by client vs server cookies.

How do I delete or invalidate a cookie?

Sending the same cookie value with ; expiresappended will not destroy the cookie. Invalidate the cookie by setting an empty value and include an expiresfield as well: Set-Cookie: token=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT Note that you cannot force all browsers to delete a cookie.

Can I force a browser to delete an expired Cookie?

Note that you cannot force all browsers to delete a cookie. The client can configure the browser in such a way that the cookie persists, even if it's expired. Setting the value as described above would solve this problem. Share Improve this answer Follow edited Sep 20 '17 at 22:59

How are cookies stored in web web servers?

web servers instruct the client to store a cookie by issuing a special HTTP header, "Set-Cookie". once cookied, every subsequent request by the client to that server will include the HTTP header, "Cookie" with the data that was stored. by default, cookies are deleted when the user quits the browser.

Why don’t my cookies get removed after sending a header?

Related: some people may wonder why their cookies do not get removed even after sending this header. In that case, have a look at cookies from other domains. For example, after deleting foo=bar; domain=www.example.com, an other cookie foo=qux; domain=.example.comwill be used. – Lekensteyn


1 Answers

Sending the same cookie value with ; expires appended will not destroy the cookie.

Invalidate the cookie by setting an empty value and include an expires field as well:

Set-Cookie: token=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT 

Note that you cannot force all browsers to delete a cookie. The client can configure the browser in such a way that the cookie persists, even if it's expired. Setting the value as described above would solve this problem.

like image 72
Lekensteyn Avatar answered Nov 02 '22 03:11

Lekensteyn