cookie、session、localStorage分别是什么?有什么作用?

本文讲解了 cookie、session、localStorage 的使用和区别

一、cookie

  1. cookie是存储在浏览器上的一小段数据,用来记录某些当页面关闭或者刷新后仍然需要记录的信息。在控制台用 「document.cookie」查看你当前正在浏览的网站的cookie。
  2. cookie可以使用 js 在浏览器直接设置(用于记录不敏感信息,如用户名), 也可以在服务端通使用 HTTP 协议规定的 set-cookie 来让浏览器种下cookie,这是最常见的做法。(打开一个网站,清除全部cookie,然后刷新页面,在network的Response headers试试找一找set-cookie吧)
  3. 每次网络请求 Request headers 中都会带上cookie。所以如果 cookie 太多太大对传输效率会有影响。
  4. 一般浏览器存储cookie 最大容量为4k,所以大量数据不要存到cookie。
  5. 设置cookie时的参数:

* path:表示 cookie 影响到的路径,匹配该路径才发送这个 cookie。expires 和 maxAge:告诉浏览器 cookie 时候过期,maxAge 是 cookie 多久后过期的相对时间。不设置这两个选项时会产生 session cookie,session cookie 是 transient 的,当用户关闭浏览器时,就被清除。一般用来保存 session 的 session_id。
* secure:当 secure 值为 true 时,cookie 在 HTTP 中是无效,在 HTTPS 中才有效
* httpOnly:浏览器不允许脚本操作 document.cookie 去更改 cookie。一般情况下都应该设置这个为 true,这样可以避免被 xss 攻击拿到cookie。[cookie 参数][简述 Cookie 是什么]

二、session

  1. 当一个用户打开淘宝登录后,刷新浏览器仍然展示登录状态。服务器如何分辨这次发起请求的用户是刚才登录过的用户呢?这里就使用了session保存状态。用户在输入用户名密码提交给服务端,服务端验证通过后会创建一个session用于记录用户的相关信息,这个 session 可保存在服务器内存中,也可保存在数据库中。

* 创建session后,会把关联的session_id 通过setCookie 添加到http响应头部中。
* 浏览器在加载页面时发现响应头部有 set-cookie字段,就把这个cookie 种到浏览器指定域名下。
* 当下次刷新页面时,发送的请求会带上这条cookie, 服务端在接收到后根据这个session_id来识别用户。

cookie 是存储在浏览器里的一小段「数据」,而session是一种让服务器能识别某个用户的「机制」,session 在实现的过程中需要使用cookie。 二者不是同一维度的东西。

三、localStorage

  1. localStorage HTML5本地存储web storage特性的API之一,用于将大量数据(最大5M)保存在浏览器中,保存后数据永远存在不会失效过期,除非用 js手动清除。
  2. 不参与网络传输。
  3. 一般用于性能优化,可以保存图片、js、css、html 模板、大量数据。

作者:若愚,未经许可禁止转载

26 thoughts on “cookie、session、localStorage分别是什么?有什么作用?”

  1. Do you have a spam problem on this site; I also am a blogger, and
    I was wondering your situation; we have developed some nice methods and we are looking to exchange solutions with others,
    why not shoot me an email if interested.

  2. This is very interesting, You are a very skilled blogger.
    I have joined your rss feed and look forward
    to seeking more of your magnificent post. Also, I’ve shared your web site in my social networks!

  3. Hey there! I know this is kind of off topic but I was wondering which blog platform are you using for this website?

    I’m getting fed up of WordPress because I’ve had issues
    with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  4. I believe this is one of the so much significant information for me.
    And i am happy studying your article. However should
    remark on some basic things, The site taste is wonderful,
    the articles is actually great : D. Excellent task, cheers

  5. I have been exploring for a little for any high quality articles or blog posts in this sort of house
    . Exploring in Yahoo I at last stumbled upon this web
    site. Studying this info So i’m satisfied to exhibit that I’ve a very
    excellent uncanny feeling I discovered just what I needed.
    I so much no doubt will make certain to don?t put out of your mind this website and give it a glance on a relentless basis.

  6. Hey there! This is my 1st comment here so I just wanted
    to give a quick shout out and say I truly enjoy reading your articles.

    Can you recommend any other blogs/websites/forums that deal
    with the same topics? Thank you so much!

  7. I’m really impressed with your writing skills as well as with
    the layout on your blog. Is this a paid theme or
    did you modify it yourself? Either way keep up the nice quality writing, it is rare to see a nice
    blog like this one today.

  8. I like the valuable information you provide for your articles.
    I’ll bookmark your weblog and check again right here frequently.
    I am relatively certain I will learn plenty of new stuff right here!

    Good luck for the following!

  9. Hi there! This post could not be written any better!
    Reading through this post reminds me of my previous room mate!
    He always kept chatting about this. I will forward this write-up to him.
    Pretty sure he will have a good read. Thanks for sharing!

  10. Normally I do not learn post on blogs, but I would
    like to say that this write-up very forced me to
    check out and do so! Your writing taste has been amazed
    me. Thank you, very nice post.

  11. Hi there! This is kind of off topic but I need some guidance from an established
    blog. Is it very difficult to set up your own blog?

    I’m not very techincal but I can figure things out pretty
    fast. I’m thinking about creating my own but I’m not sure where to begin.
    Do you have any ideas or suggestions? With thanks

  12. Hi there, just became alert to your blog through Google, and found that it’s
    truly informative. I’m going to watch out for brussels.

    I’ll be grateful if you continue this in future.
    A lot of people will be benefited from your writing.

    Cheers!

  13. Hi there! This is my 1st comment here so I just wanted to give
    a quick shout out and tell you I really enjoy reading through your articles.
    Can you suggest any other blogs/websites/forums that go over the same subjects?
    Appreciate it!

  14. Heya i am for the first time here. I found this board and I in finding
    It truly helpful & it helped me out a lot. I am hoping to give something again and
    aid others like you helped me.

  15. Excellent blog right here! Also your web site so much
    up fast! What host are you the usage of? Can I get your associate link in your host?
    I desire my website loaded up as fast as yours lol

  16. Hello There. I found your weblog the usage of msn. That is a very well written article.
    I will make sure to bookmark it and come back to learn extra of your helpful info.
    Thanks for the post. I’ll definitely return.

发表评论

电子邮件地址不会被公开。