Junior
206
Can a server modify (add, delete) cookies?
Answer from AI
sobes.tech AI
Yes, possibly. The server manages cookies through HTTP headers Set-Cookie in the response to the client.
For example:
Setting a new cookie:
HTTP/1.1 200 OK
Set-Cookie: sessionid=abcde12345; HttpOnly; Secure; Path=/; Expires=Fri, 31 Dec 2021 23:59:59 GMT
// The server sends a Set-Cookie header,
// which causes the browser to create or update a cookie named sessionid.
Modifying the value of an existing cookie:
A new Set-Cookie header is sent with the same cookie name but a new value.
HTTP/1.1 200 OK
Set-Cookie: sessionid=vwxyz67890; HttpOnly; Secure; Path=/; Expires=Fri, 31 Dec 2021 23:59:59 GMT
// The browser updates the value of the sessionid cookie.
Deleting a cookie:
Deletion occurs by setting a cookie with the same name but with an expired date (Expires in the past) or setting the maximum age to zero (Max-Age=0).
HTTP/1.1 200 OK
Set-Cookie: sessionid=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/
// The browser deletes the sessionid cookie as its expiration date has passed.
The server cannot directly "delete" a cookie from the client machine, but it sends instructions to the browser via the Set-Cookie header, which the browser is obliged to execute.