Front-end learning: the difference between cookie and session

The difference between Cookie and Session

1、cookie

1. What are cookies and sessions?A cookie is something that stores data in the browser, so why do we use it to store, js variables can also store data, why not use him? Because if we use js variables to store data, the data will be cleared when the page is closed. So very inconvenient.
2. So how do we keep the login state (that is, let the data be stored in it, and it will not be closed because the page is closed), it is impossible to do it according to the normal http protocol, because the http protocol is a context-free protocol, that is, if When we log in on one page and change to another page, he will refresh and enter a new page. The two pages have nothing to do with each other, so you log in on one page and have nothing to do with the other page.
3. So we need something that can store data persistently on the front-end page, and this is handed over to cookies.

1) Cookies are limited, and there are domain name restrictions
2) Cookies are stored in the browser, not on the page, and can be stored for a long time. Even if it exists in the browser, it is stored under a different domain name.
Cookie disadvantage:
If the cookie in my browser is copied by a bad guy, he can log in to my account on his computer.

2、Session

The session information is stored on the server, and the information stored in the session is difficult to be copied.
Session disadvantages:
(1) When the number of users is very large, the server will consume a lot of resources.
Because there is more than one server at the back end, the user's login information generally only exists on one machine. When the user's login operation is performed on a certain machine, the information will be stored on that machine.

2. Summary

The above is a record of my learning process, which can be used for reference. If you have any questions, please correct me.

Guess you like

Origin blog.csdn.net/TotoroChinchilla/article/details/125615319