当前位置:网站首页>Cookie and Session usage
Cookie and Session usage
2022-08-03 11:10:00 【51CTO】
One.Why are cookies needed?
A and B both have their own Jingdong accounts, and they have successfully logged in locally (7 days password-free login). At this time, A will send his shopping cart to level up.For B, B cannot view the contents of A's shopping cart by accessing the link with a browser. B may directly enter his own shopping cart or prompt B to log in again and jump to the login page.This scenario shows that the server knows who the client is accessing.But we all know that http is stateless, we need to use cookies to mark who the client is and send it to the server.
II.Cookies and Sessions
A cookie is a set of key-value pairs stored locally.How Cookie works: it is a set of key-value pairs generated by the server and sent to the client and saved locally on the client when the client accesses the server.definition).When the browser visits again, the browser will automatically bring the cookie, so that the server can determine who the server is based on the content of the cookie.
Although the cookie solves the requirement of "maintaining state" to a certain extent and makes up for the stateless defect of the http protocol, the cookie itself only supports a maximum of 4096 bytes; and the cookie itselfIt is stored locally on the client and can be intercepted or intercepted, but there is no guarantee of security.Therefore, at this time, it is necessary to use the session to help solve the problem of maximum bytes and the security problem of saving locally. The session saves the user information on the server.
So, if the session is kept to the server, how does the client bridge the session of the server through the cookie?
This raises the question of how cookies and sessions help?We can assign a unique id to each client's cookie, so that when the user accesses, through the cookie, the server knows who is coming.Then we save the user's private information on the server for a period of time according to the id of different cookies.
In summary: cookie makes up for the lack of stateless http, letting the server know who is coming; but the cookie is stored locally in the form of text, which is more secure.Poor; so we use cookies to identify different users, and correspondingly save the user's private information in the session and the text that can exceed 4096 bytes.
Three.Single use of cookies?
Four.The combined application of Cookie and Session?
边栏推荐
猜你喜欢
随机推荐
What is the ERC20 token standard?
【JDBC以及内部类的讲解】
ScrollView嵌套RecyclerView滚动冲突
【网络原理的概念】
像用户体验设计师一样思考
[Explanation of JDBC and inner classes]
507. 完美数
build --repot
完全背包问题的思路解析
MySQL数据库实战(1)
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
mysql数据库定时备份占用大量线程,导致全局锁表,有啥好的解决方法么
MATLAB程序设计与应用 2.6 字符串
干货!一种被称为Deformable Butterfly(DeBut)的高度结构化且稀疏的线性变换
【Star项目】小帽飞机大战(九)
直播弱网优化
科普大佬说 | 黑客帝国与6G有什么关系?
机器学习概述
MySQL数据库基本使用
跨链桥协议 Nomad 遭遇黑客攻击,损失超 1.5 亿美元