• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

cookie、sessionStorage和localStorage有什么区别?


Recommended Posts

数据存储位置

这三者都存储在本地浏览器中。

区别在于cookie是服务器写的,而sessionStorage和localStorage是前端写的。

生命周期

cookie的生命周期是由服务器写入时设定的。

除非手动清除,否则localStorage始终存在。

页面关闭后,SessionStorage会自动清除。

存储大小

cookie的存储空间约为4KB,sessionStorage和localStorage约为5M。

数据共享

Cookie、sessionStorage、localStorage数据共享都遵循同源原则(协议、域名、端口号),sessionStorage必须在同一个页面。

发送请求是否携带信息

前端向后端发送数据时,会自动携带cookie数据,而sessionStorage和localStorage不会。

应用场景

Cookie通常用于存储登录验证信息、会话ID和令牌。

SessionStorage可以用来检测用户是否通过刷新进入页面,比如音乐播放器的进度条。

LocalStorage常用于存储不易更改的数据,在一定程度上缓解了服务器的压力。

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now