Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which way to create cookie, by frontend or backend?

Tags:

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.

like image 414
vancewang Avatar asked Sep 28 '14 07:09

vancewang


People also ask

Are cookies front end or backend?

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.

How are HTTP cookies created?

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.

How are cookies set?

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).


1 Answers

There are a few considerations:

  1. 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.

  2. 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.

like image 121
jfriend00 Avatar answered Sep 18 '22 23:09

jfriend00