The Cookie and Session
1. cookie
cookie
isThe clientA Web server stores a plain text file on a computer or mobile device, a small piece of data that the server sends to a Web browser. Generally, the size is limited to 4kb.cookie
Is a built-in mechanism for passing text values back and forth between the server and the client, which the server can base oncookie
Track user visits to different pages.
When receiving an HTTP request from a client, the server can send a set-cookie header with a response, and then send the request with the Cookie along with the HTTP request header.
The usefulness of the cookie
- Session management: User account password
- Personalization: User preference Settings
- Tracking: Records and analyzes user behavior
The characteristics of the cookie
- The size is limited to 4KB
- It consumes bandwidth on the network
- It’s not secure without encryption
- Using JS to manipulate cookies is complicated
2. Session
Session
Is a server-side solution that maintains state through the server.Session
Is a special object created by the server to hold user state. The client requests the server, and the server allocates a chunk of memory for the request.Session
Compensating for the stateless nature of HTTP.
Session creation process
When the browser accesses the server for the first time, the server creates a Session object (which has a unique ID, the SessionID). The server returns the SessionID to the browser as a cookie.
When the browser visits the server again, it sends the sessionID, and the server can find the corresponding session object based on the sessionID.
The disadvantage of the Session
Server A stores sessions, that is, after load balancing is performed, if the number of visits from server A surges within A period of time, the Session will be forwarded to server B for access. However, server B does not store sessions of server A, which results in Session invalidity.
Web Storage
If you want to manipulate sessionStorage of a domain name, use window.sessionstorage; If you want to manipulate localStorage for a domain name, use window.localstorage.
1. localStorage
Read-only localStorage allows access to a Document object Storage, and the stored data will be stored in the browser session.
2. sessionStorage
The sessionStorage property allows you to access a sessionStorage object corresponding to the current source. It is similar to localStorage except that data stored in localStorage does not have an expiration date, while data stored in sessionStorage is cleared when the page session ends.
Similarities and differences
The same
localStorage
andSessionStorage
Both are objects used to store temporary client information.- Only string objects can be stored
- Different browsers cannot share
localStorage
withSessionStroage
Information in. Different pages in the same browser (homologous pages) can share the samelocalStorage
, but not sharedsessionStorage
.
The difference between
localStorage
The life cycle is permanent unless cleared by the userlocalStorage
Information.sessionStorage
Is the current window or TAB. End once the window is permanently closed.
References
- LocalStorage and sessionStorage local store | punch a stop skills every day
- Session, Cookie, Token
- Understand cookies and Session mechanisms
- Storage | MDN
- LocalStorage | MDN
- What is the difference between sessionStorage and localstorage