Answer the question
In order to leave comments, you need to log in
Should everything be stored in SESSION, or is it better to store something in COOKIE?
[Moderator, there is no clear answer on the Internet. And the answer "what you want, then store" does not quite suit me. Thank you for understanding]
Hello! This question has been tormenting me for several days, but there is no clear answer on the Internet. I understand how cookies and sessions work. Thanks to the key, usually called PHPSESSID, the server can identify the user and create / get the corresponding session, which stores some data .
So, cookies are on the client, sessions are on the server.
But where to store what? It is clear that it is better to store any secret data in the session, but what exactly should be stored in cookies? The moderator, before deleting my question, wrote something like: "what you want, then keep it." It's still not clear! Why write something to cookies when you can not load the client with all sorts of cookies, but leave him only PHPSESSID to get by with just a session? After all, the setcookie() function was created for something. Where it is necessary to use cookies, except for any tokens and other information due to which access to the session is provided. Where is a cookie indispensable?
Answer the question
In order to leave comments, you need to log in
After all, the setcookie() function was created for something.
You can put harmless data in the cookie:
In a cookie, data can be stored for a very long time, while in a session on the server, data will be stored only during the session itself. This is ideal when you want to save some data for unauthorized users, for example: a person came to your site, the site uses notifications that ask before displaying "does the person want to receive your notifications", the answer to this question is best stored in a cookie. The next time this user visits your site and he will have a new session, but the information that he does not want to see notifications will be stored in the cookie.
And how long can you afford to keep sessions on the server? if the person doesn't come back? If this is a bot, which one generated another session for you? Cookies help to save some values for a long time, according to which after a long time we will be able to understand that we have already had a person and chose such and such options. On the other hand, cookies are sent on every request, whether it's a page, image, script, stylesheet, ajax request. All or almost all cookies are always sent. so you need to be careful with them, not to mention the fact that they can be faked or read. You also need to take into account the size of the data sent to the cookies, I saw many examples when nginx fell off due to large cookies, the user was lost, nothing was really seen in the logs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question