Per I understand, cookie is some way to make our webapp stateful.
As cookies can be created both in javascript (frontend) and from http response (by backend), so is there any principle when cookies should be created by frontend and when by backend?
Is some user scenario can be given, it would be great.
Cookies are tiny pieces of data that the backend can store in the user's browsers. User tracking, personalization, and most important, authentication, are the most common use cases for cookies.
Cookies are created to identify you when you visit a new website. The web server — which stores the website's data — sends a short stream of identifying info to your web browser. Browser cookies are identified and read by “name-value” pairs. These tell cookies where to be sent and what data to recall.
Cookies are set using the Set-Cookie header field, sent in an HTTP response from the web server. This header field instructs the web browser to store the cookie and send it back in future requests to the server (the browser will ignore this header field if it does not support cookies or has disabled cookies).
There are a few considerations:
Where is the content for the cookie created? If it's a session id, then that's probably created on the server so the cookie would be created there. If it's a user viewing preference that isn't stored server-side, then that's probably set in the client and the cookie would be set there.
Server-side cookies can be set with additional security (called http-only) that makes them visible only to servers, not to client-side javascript, but they are still stored by browsers to represent a particular client.
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