The difference between Session Storage, Local Storage and Cookies

Session Storage, Local Storage and Cookies are three ways to store data on the client side. The differences between them are as follows:

1. Different storage sizes:

The storage capacity of Session Storage is usually smaller than that of Local Storage (about 5MB), while the storage capacity of Cookies is usually smaller (about 4KB).

2. Different access scopes:

Session Storage and Local Storage can only be accessed by the browser's JavaScript, while Cookies can not only be accessed by JavaScript, but can also be read by the server.

3. Different life cycles:

The data in Session Storage is only valid during the current session, that is, all data will be deleted when the user closes the browser window; the data in Local Storage will not expire unless it is explicitly cleared or the user clears the browser cache; Cookies can Set the expiration time, which will be valid until the expiration time.

4. Automatically sent to the server:

Cookies are automatically sent to the server in every HTTP request, so they can be used for persistent login or tracking user behavior; Session Storage and Local Storage are not automatically sent to the server, so they are not suitable for persistent login and other requirements.

5. Different security:

Since Cookies are stored on the client side, they may be subject to security issues such as CSRF attacks and XSS attacks; and Session Storage and Local Storage may be subject to XSS attacks.

It should be noted that these three storage methods are relatively unsafe because the stored data can be accessed by malicious JavaScript code. If sensitive information is stored, additional measures such as encryption are recommended to increase the level of protection.

Guess you like

Origin blog.csdn.net/weixin_43534452/article/details/131438729