Session variables and cookies seem very similar to me. I understand the technical differences, but how do you decide when to use one vs. the other?
相关问题
- Angular RxJS mergeMap types
- Google Apps Script: testing doPost() with cURL
- How to instantiate Http service in main.ts manuall
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- Multiple Django sites on the same domain - CSRF fa
相关文章
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- Is a unicode user agent legal inside an HTTP heade
- git: retry if http request failed
- fetch: Getting cookies from fetch response
- Flutter - http.get fails on macos build target: Co
- How do I send cookies with request when testing Fl
- Is ![removed] reliable?
- C# HttpClient.SendAsync always returns 404 but URL
Sessions are stored on the server, which means clients do not have access to the information you store about them. Session data, being stored on your server, does not need to be transmitted in full with each page; clients just need to send an ID and the data is loaded from the server.
On the other hand, cookies are stored on the client. They can be made durable for a long time and would allow you to work more smoothly when you have a cluster of web servers. However, unlike sessions, data stored in cookies is transmitted in full with each page request.
Avoid storing data in cookies
What you can store in session data depends on the amount of data and number of users you have.
no_of_users
*size_of_session_data
must be less than the free memory available on your server.Cookies can persist longer than a single session. However, cookies may also be deleted by the user, or you may have a user whose browser does not accept cookies (in which case only a server-side session will work).
Use sessions only if the data is too big for cookies or if the data is so big that it would decrease the performance if you used cookies.
For example, if you are saving smaller data then the size of a session ID in your cookie, like two login tokens or something similar... Then I don't see why you would use sessions over cookies.
Also note that PHP session files are saved to disk by default, compared to cookies, which are saved only on the client side.
One of the drawbacks of PHP sessions is how session handling works. Specifically, only one process/request can have a session open for writing at a time. Upon
the session file is locked. If more processes come along, the rest pile up and wait their turn.
In other words, if you are using AJAX on a page to update several elements - you do not want the AJAX requests opening up the same session - they will be forced into a queue and if one of those requests get stuck - it will not release the session - resulting in a browser hang where opening up a new tab or window only puts another unfillable request into the queue on the server. Using
as soon as possible to release the session is a partial work-around.
A long running request with a user getting bored and opening up more windows could have the same browser hanging effect.
I recommend avoiding PHP sessions.
Sessions are stored on the server side. If a visitor stores something in a cookie, the browser will send the user information for every request made.
This tends to consume a lot of servers computer time and slowing the user's experience. Some browsers also do not support cookies giving more advantage to sessions over cookies... I strongly recommend sessions.
This might help: Cookies (php.net)
Your definite Guide
N.B - A cookie is stored on users' browsers, and a session is stored on your hosting server machine.
When to Use
Use a cookie when you want your application to remember user's data always, even when they have closed their browsers. E.g whenever you type www.facebook.com it takes you to your account, even when your browser has been closed and re-opened.
Because any data kept in a session is cleared off once you close your browser.
Use a cookie when the user information to be stored is much larger than normal. ... With a session, if you have a larger user base, like Facebook, think of how it will look storing all user sessions on the hosting machine.
Use a session when the user information to be stored is not larger than normal, and you don't want the public to have access to your user variables...