简述 Cookie 是什么

Cookie 是什么

  1. Cookie 是浏览器访问服务器后,服务器传给浏览器的一段数据。

  2. 浏览器需要保存这段数据,不得轻易删除。

  3. 此后每次浏览器访问该服务器,都必须带上这段数据。

Cookie 就是这么简单,这就是 Web 开发里 Cookie 的含义。

如何使用 Cookie

Cookie 一般有两个作用。

第一个作用是识别用户身份。

比如用户 A 用浏览器访问了 a.com,那么 a.com 的服务器就会立刻给 A 返回一段数据「uid=1」(这就是 Cookie)。当 A 再次访问 a.com 的其他页面时,就会附带上「uid=1」这段数据。

同理,用户 B 用浏览器访问 a.com 时,a.com 发现 B 没有附带 uid 数据,就给 B 分配了一个新的 uid,为2,然后返回给 B 一段数据「uid=2」。B 之后访问 a.com 的时候,就会一直带上「uid=2」这段数据。

借此,a.com 的服务器就能区分 A 和 B 两个用户了。

第二个作用是记录历史。

假设 a.com 是一个购物网站,当 A 在上面将商品 A1 、A2 加入购物车时,JS 可以改写 Cookie,改为「uid=1; cart=A1,A2」,表示购物车里有 A1 和 A2 两样商品了。

这样一来,当用户关闭网页,过三天再打开网页的时候,依然可以看到 A1、A2 躺在购物车里,因为浏览器并不会无缘无故地删除这个 Cookie。

借此,就达到里记录用户操作历史的目的了。

(上面的例子只是为了让大家了解 Cookie 的作用而构想出来的,实际的网站使用 Cookie 时会更谨慎一些。)

作者:方应杭

76 thoughts on “简述 Cookie 是什么”

  1. Hello, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you prevent it, any plugin or anything you can recommend? I get so much lately it’s driving me insane so any assistance is very much appreciated.|

  2. After going over a number of the blog posts on your blog, I truly appreciate your way of writing a blog. I book marked it to my bookmark website list and will be checking back soon. Please check out my website too and let me know what you think.|

  3. Excellent goods from you, man. I’ve understand your stuff previous to and you are just extremely magnificent. I actually like what you have acquired here, really like what you’re saying and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can’t wait to read much more from you. This is actually a terrific website.|

  4. Hey are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be greatly appreciated!|

  5. fantastic submit, very informative. I ponder why the other experts of this sector don’t realize this. You should continue your writing. I’m confident, you’ve a great readers’ base already!|

  6. Great goods from you, man. I’ve understand your stuff previous to and you are just too wonderful. I really like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it entertaining and you still take care of to keep it smart. I can’t wait to read far more from you. This is actually a great site.|

  7. I have read a few excellent stuff here. Certainly value bookmarking for revisiting. I surprise how much attempt you set to make this type of excellent informative site.|

  8. I have been exploring for a little bit for any high-quality articles or blog posts in this sort of house . Exploring in Yahoo I finally stumbled upon this site. Studying this info So i am happy to exhibit that I’ve an incredibly excellent uncanny feeling I discovered exactly what I needed. I such a lot surely will make sure to do not fail to remember this web site and give it a look regularly.|

  9. of course like your web site but you have to check the spelling on quite a few of your posts. Many of them are rife with spelling problems and I to find it very troublesome to tell the truth on the other hand I’ll definitely come back again.|

  10. Good day! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche. Your blog provided us valuable information to work on. You have done a extraordinary job!|

  11. I believe what you posted made a great deal of sense. But, think about this, what if you added a little content? I mean, I don’t want to tell you how to run your website, but suppose you added a post title to possibly get people’s attention? I mean BLOG_TITLE is kinda plain. You might glance at Yahoo’s home page and note how they create news titles to get viewers interested. You might add a video or a related pic or two to grab people interested about what you’ve got to say. In my opinion, it might make your blog a little livelier.|

  12. I blog often and I truly appreciate your information. This article has truly peaked my interest. I am going to bookmark your site and keep checking for new details about once a week. I opted in for your RSS feed as well.|

  13. I’ve been surfing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the net will be much more useful than ever before.|

  14. Hi! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no data backup. Do you have any methods to prevent hackers?|

  15. Thank you, I have recently been looking for information approximately this subject for a long time and yours is the greatest I have discovered till now. But, what concerning the conclusion? Are you sure about the source?|

  16. Piece of writing writing is also a fun, if you be acquainted with after that you can write otherwise it is difficult to write.|

  17. Having read this I believed it was really informative. I appreciate you finding the time and energy to put this short article together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worthwhile!|

  18. I do not know whether it’s just me or if perhaps everybody else experiencing problems with your blog. It appears as though some of the written text in your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them too? This could be a issue with my browser because I’ve had this happen before. Thank you|

  19. I was wondering if you ever thought of changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two images. Maybe you could space it out better?|

  20. I was pretty pleased to discover this website. I need to to thank you for ones time just for this wonderful read!! I definitely savored every part of it and I have you saved as a favorite to check out new information in your website.|

  21. Hey there I am so glad I found your website, I really found you by mistake, while I was browsing on Askjeeve for something else, Nonetheless I am here now and would just like to say kudos for a marvelous post and a all round interesting blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic job.|

  22. I was recommended this blog by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty. You’re incredible! Thanks!|

  23. Great beat ! I would like to apprentice at the same time as you amend your website, how could i subscribe for a blog site? The account aided me a applicable deal. I had been a little bit familiar of this your broadcast offered brilliant transparent idea|

  24. Wow, marvelous blog format! How long have you been running a blog for? you made blogging glance easy. The entire glance of your website is magnificent, let alone the content!

  25. You actually make it seem so easy along with your presentation however I find this topic to be really one thing that I believe I would never understand. It sort of feels too complicated and very extensive for me. I’m having a look ahead for your subsequent post, I will attempt to get the cling of it!

  26. Ahaa, its good conversation on the topic of this paragraph here at this web site, I have read all that, so at this time me also commenting at this place.|

  27. Hi would you mind letting me know which hosting company you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good internet hosting provider at a honest price? Thanks a lot, I appreciate it!|

  28. I do consider all the ideas you’ve presented on your post. They are very convincing and can definitely work. Nonetheless, the posts are too quick for starters. Could you please prolong them a bit from next time? Thanks for the post.

  29. Thanks for every other wonderful post. Where else could anyone get that kind
    of information in such a perfect way of writing?
    I’ve a presentation next week, and I’m on the search for
    such info.

  30. I all the time used to read article in news papers but now as I am a user of web so from now I am using net
    for posts, thanks to web.

发表评论

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