Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session与cookie #13

Open
nianxiongdi opened this issue Aug 29, 2019 · 0 comments
Open

session与cookie #13

nianxiongdi opened this issue Aug 29, 2019 · 0 comments

Comments

@nianxiongdi
Copy link
Owner

nianxiongdi commented Aug 29, 2019

cookie和localstorage有什么区别?
答:1. cookie数据大小不能超过4k
2.cookie在服务端和客户端之间来回传递
3.cookie可以设置很多属性,max-age,path,expires等
4.localStorage存储持久数据,浏览器关闭后数据不丢失除非主动删除数据
5.localStorage存储的数据容量更大
cookie的字段:
key:设置的cookie的key。
value:key对应的value。
max_age:改cookie的过期时间,如果不设置,则浏览器关闭后就会自动过期。
expires:过期时间,应该是一个datetime类型。
domain:该cookie在哪个域名中有效。一般设置子域名,比如cms.example.com。
path:该cookie在哪个路径下有效。

  1. 存储
    答:cookie:保存在浏览器端,只能保存字符串类型,以文本的方式,单个cookie保存的数据不能超过4kb
    session:保存在服务器端,能支持任何类型的对象,大小没有限制。
    session的安全性大于cookie
    WebStorage:localStorage(本地存储)、sessionStorage(会话存储)
    localStorage:生命周期是永久的,关闭页面或浏览器之后localStorage中的数据也不会消失,除非主动删除数据。
    sessionStorage:仅在当前会话下有效。关闭窗口就会被销毁。
    localStorage和sessionStorage的存储数据大小一般都是:5MB
    localStorage和sessionStorage都保存在客户端,不与服务器进行交互通信。
    localStorage和sessionStorage只能存储字符串类型,对于复杂的对象可以使用ECMAScript提供的JSON对象的stringify和parse来处理
    localStorage:window.localStorage;;sessionStorage:window.sessionStorage;
    localStoragese:常用于长期登录(+判断用户是否已登录),适合长期保存在本地的数据。sessionStorage:敏感账号一次性登录;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant