【Programming】Cookie and Session

Cookie and Session are two commonly used state management mechanisms in web development, which are used to transfer and save user state information between the client and the server.

Cookie

A cookie is a small piece of text information stored on the client, sent by the server to the client through the Set-Cookie field in the HTTP response header, and returned to the server by the client through the Cookie field in the HTTP request header. Scenarios for using cookies include: saving user login status, recording user browser preferences, tracking user behavior, etc. The advantages of cookies are convenient storage, cross-domain transmission, and good compatibility, but the disadvantage is that they are easy to be tampered with and may leak user privacy.

Session

Session is a set of user status information stored on the server side. It is created by the server side when the user visits for the first time, and a unique Session ID is assigned, which is passed to the client side through Cookie or URL parameters in each subsequent request. The client will carry the Session ID back to the server in the next request, and the server finds the corresponding Session object through the Session ID, and then obtains or stores user status information from it. The usage scenarios of Session include: saving user login status, storing user shopping cart information, maintaining user's visit history on the website, etc. The advantages of Session are high security, good reliability, and support for large amounts of data storage, but the disadvantages are that it takes up server memory, is difficult to transfer across domains, and is easy to be hijacked by Session.

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/130036873