What is the way to create a cookie, frotend or backend?

In understanding, cookies are a way to make our webapp work.

Since cookies can be created both in javascript (frontend) and in response to http (by backend), is there also any principle when cookies should be created by the interface and when by the backend?

Is it possible to specify some kind of custom script, that would be great.

+7
javascript
source share
3 answers

There are several considerations:

  • Where is the content for the cookie generated? If this is a session identifier, then it is probably created on the server, so a cookie will be created there. If the user views preferences that are not stored on the server side, then it is possible that the cookie is installed in the client and the cookie will be set there.

  • Server-side cookies can be set with added security (called http only), which makes them visible only to servers, not client-side javascript, but they are still stored by browsers to represent a specific client.

+5
source share

This is the same. Use whatever is convenient. For example, if you are doing something in JS (in the interface) and want to save a cookie, store it with JS. The same goes for an external server.

This can help.

  • Cookies - PHP vs Javascript
+1
source share

Note that server-side code is executed before client-side code.

I came across a situation like this:

My internal code tracks every movement on the website, and the indicator for the visitor is a cookie. However, for the initial visit, when the user does not have a cookie, my function will save this movement with an undefined indicator, because the user's request is processed before the generation of cookies.

So, in this case, I would go with a cookie file.

In the meantime, I like to create client-side cookies when I need to fix a broken WordPress website, and I use the external Node.JS API as glue.

0
source share

All Articles