session and cookie components

Cookie introduction

The origin of cookies

Everyone knows that the HTTP protocol is stateless.

Stateless means that each request is independent, its execution status and results are not directly related to the previous request and the subsequent request, it will not be directly affected by the previous request response, nor will it directly affect the back Request response.

For the server, each request is completely new.

The state can be understood as the data generated by the client and the server in a certain session, and the stateless thinks that these data will not be retained. The data generated in the session is what we need to save, that is to say "maintain state". Therefore, cookies are born in such a scene.

What is a cookie

In fact, Cookie is a key-value structure, similar to a dictionary in Python. With the server-side response sent to the client browser. The client browser will then save the cookie and send the cookie to the server when the server is accessed again. A cookie is a key-value pair created by the server and then sent to the client in response. The client will save the cookie and mark the source of the cookie (which server's cookie). When the client sends a request to the server, all the server cookies are included in the request and sent to the server, so that the server can identify the client!

 

The principle of cookies

The working principle of the cookie is: the content is generated by the server, and the browser saves it locally after receiving the request; when the browser visits again, the browser will automatically bring the cookie, so that the server can judge this is "who ""

 

Cookie specification 

  •  The maximum cookie size is 4KB; 
  •  A server can save up to 20 cookies on the client browser; 
  •  A browser can store up to 300 cookies;  

The above data is only the Cookie specification of HTTP, but in the battle of browsers, some browsers may "expand" the cookie specification in order to defeat their opponents and show their ability. For example, the size of each cookie is 8KB, Up to 500 cookies etc. can be saved! But there is no possibility to fill up your hard drive! 
Note that cookies are not shared between different browsers. That is to say, when you use IE to access the server, the server will send cookies to IE, and then save them by IE. When you use FireFox to access the server, it is impossible to send cookies saved by IE to the server.

 

Cookie coverage 

  If the server sends duplicate cookies, the original cookies will be overwritten. For example, the first request from the client to the server sends the cookie: Set-Cookie: a = A; the second request from the server sends: Set-Cookie: a = AA, then the client leaves only one cookie, namely: a = AA.

 

View cookies in browser

Press F12 in the browser and click network --- cookies to see

 

 

When you first visit, first determine that the username and password are matched with the database. If there is no problem, you will be sent a cookie key-value pair. This cookie contains all your personal information. When your browser sends the same request about this website again, it will first match according to your key, and then display your value if there is any.

Guess you like

Origin www.cnblogs.com/zhangrenguo/p/12717270.html