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 模板、大量数据。

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

57 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. I am actually thankful to the owner of this site who has shared this great article at at this place.

  3. 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!

  4. 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.

  5. 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

  6. 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.

  7. 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!

  8. 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.

  9. 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!

  10. 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!

  11. 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.

  12. 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

  13. Hi there, You have done a fantastic job.
    I’ll definitely digg it and personally recommend to my friends.
    I’m sure they’ll be benefited from this web site.

  14. 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!

  15. 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!

  16. 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.

  17. 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

  18. 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.

  19. I found your weblog site on google and check a number of of your early posts. Continue to maintain up the very good operate. I just further up your RSS feed to my MSN Information Reader. Seeking ahead to studying extra from you later on!?

  20. Does your site have a contact page? I’m having trouble locating
    it but, I’d like to shoot you an e-mail. I’ve got some suggestions
    for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it expand over time.

  21. I want to point out my respect for your kind-heartedness giving support to people who should have guidance on this area of interest. Your very own commitment to getting the message around had become remarkably good and have truly empowered people like me to arrive at their pursuits. The insightful facts can mean much a person like me and further more to my colleagues. Regards; from each one of us.

  22. I enjoy you because of every one of your hard work on this blog. Debby delights in managing investigations and it’s easy to understand why. I know all regarding the dynamic manner you present great tactics through your blog and therefore improve contribution from people on that concept plus our favorite simple princess is actually becoming educated a great deal. Take pleasure in the rest of the new year. Your conducting a pretty cool job.

  23. Howdy! This blog post could not be written much better!
    Going through this post reminds me of my previous roommate!
    He continually kept preaching about this. I’ll send this article to him.
    Fairly certain he’s going to have a good read. Many thanks
    for sharing!

  24. I simply wanted to construct a small message in order to appreciate you for all of the nice guides you are placing on this site. My prolonged internet search has at the end of the day been compensated with sensible facts and strategies to share with my visitors. I would repeat that many of us site visitors actually are undoubtedly lucky to be in a useful place with so many outstanding professionals with very beneficial things. I feel really fortunate to have used your entire website and look forward to some more thrilling moments reading here. Thank you once more for everything.

  25. I’m also commenting to let you know what a perfect encounter my friend’s girl undergone going through your web site. She picked up numerous details, most notably what it’s like to have a very effective helping heart to have most people smoothly understand a number of complicated matters. You truly did more than my expected results. Thank you for offering these great, trustworthy, revealing as well as fun thoughts on the topic to Ethel.

  26. I want to show my gratitude for your generosity for those who have the need for help with this situation. Your real dedication to getting the solution all through appeared to be extremely insightful and has consistently encouraged others like me to realize their ambitions. Your amazing valuable facts indicates a lot to me and much more to my fellow workers. Regards; from all of us.

  27. Needed to create you one little bit of observation to finally give many thanks again just for the lovely views you have shared at this time. It has been really pretty generous with people like you to offer openly just what a few individuals could possibly have supplied as an ebook to generate some profit on their own, most notably given that you might well have tried it in the event you considered necessary. These ideas likewise worked to become a great way to recognize that the rest have the identical desire similar to my own to see a good deal more with regards to this matter. I am certain there are many more pleasant periods ahead for individuals who look into your blog post.

  28. What’s up to all, because I am really eager of reading this webpage’s post to be
    updated on a regular basis. It carries nice data.

  29. Helpful information. Fortunate me I found your site accidentally, and I’m
    shocked why this twist of fate didn’t happened in advance!
    I bookmarked it. 34pIoq5 cheap flights

  30. I precisely needed to appreciate you all over again. I do not know the things I would’ve undertaken in the absence of the type of tricks shared by you directly on that theme. It had become a very frustrating circumstance in my circumstances, but taking note of the well-written fashion you solved that made me to weep for contentment. Now i am thankful for your guidance as well as have high hopes you know what a powerful job you were putting in training others thru your web page. I am sure you haven’t got to know any of us.

  31. Remarkable issues here. I’m very glad to see your article.

    Thanks so much and I’m looking forward to touch you. Will you kindly drop me a e-mail?

  32. Hi, i read your blog occasionally and i own a similar one and i was just curious
    if you get a lot of spam remarks? If so how do you reduce
    it, any plugin or anything you can recommend? I get so
    much lately it’s driving me mad so any assistance is very much appreciated.

  33. Greetings! I know this is somewhat off topic but
    I was wondering which blog platform are you using for this site?

    I’m getting sick and tired of WordPress because
    I’ve had issues with hackers and I’m looking at options for another platform.
    I would be awesome if you could point me in the direction of a good
    platform. cheap flights 34pIoq5

  34. I precisely wished to thank you so much all over again. I do not know the things that I would have undertaken without the type of solutions documented by you relating to this industry. This has been a real challenging circumstance in my opinion, nevertheless spending time with a professional manner you resolved that forced me to cry for contentment. I will be happy for your support and even sincerely hope you realize what a powerful job that you are accomplishing training some other people via a site. Most likely you have never encountered all of us.

  35. Aw, this was an exceptionally nice post. Taking a few minutes and actual effort to make a superb article… but what can I say… I procrastinate a whole
    lot and don’t seem to get nearly anything done.

  36. Hey! Do you use Twitter? I’d like to follow you if
    that would be ok. I’m definitely enjoying your blog and look forward
    to new posts.

  37. I enjoy you because of your whole hard work on this blog. Gloria really loves making time for investigation and it is easy to see why. A number of us learn all about the dynamic tactic you create practical tips via your blog and even attract response from the others on this subject plus my girl is really studying so much. Enjoy the rest of the new year. Your carrying out a pretty cool job.

  38. Hi there just wanted to give you a quick heads up.
    The words in your content seem to be running off the
    screen in Ie. I’m not sure if this is a format issue or
    something to do with web browser compatibility
    but I figured I’d post to let you know. The layout look great
    though! Hope you get the problem solved soon. Cheers

  39. I definitely wanted to develop a note so as to thank you for the splendid items you are giving out at this site. My time intensive internet look up has now been rewarded with really good information to share with my guests. I ‘d believe that we site visitors are very fortunate to be in a fantastic place with very many lovely people with valuable plans. I feel quite lucky to have discovered your web page and look forward to some more fabulous moments reading here. Thanks a lot again for a lot of things.

  40. My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on various websites for
    about a year and am anxious about switching to another platform.
    I have heard good things about blogengine.net. Is there a way I can import
    all my wordpress posts into it? Any help would be greatly appreciated!

  41. Wow, amazing blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your
    web site is great, as well as the content!

  42. Hello! I just want to give an enormous thumbs up for the good data you may have here on this post. I shall be coming back to your weblog for extra soon.

发表评论

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