This is the 11th day of my participation in the August More Text Challenge

background

Before HTML5 came out, cookies were used as a method of local storage. Later html5 provides – in the client localStorage data function, it is Web Storage, specifically, Web Storage is divided into two types :localStorage and sessionStotage in the client to store data, The purpose of WebStorage is to overcome some of the limitations imposed by cookies. When data needs to be tightly controlled on the client, it does not need to continuously send data back to the server.

cookie

Cookie, as the name suggests, should be very small in size, cookie is very small, its size is limited to about 4KB, was invented by a former netscape employee in 1993. It is mainly used to save login information, for example, you can see and remember the password when you log in to a website market, which is realized by storing a section of data to identify the user’s identity in the cookie.

localStorage

LocalStorage is a new HTML 5 standard technology, of course, back in the IE6 era there was a userData thing for localStorage, and at that time considering browser compatibility, the more general scheme is to use flash. LocalStorage is now supported by most browsers.

sessionStorage

SessionStorage. Similar to localStorage, but the life cycle of saving data is different from localStorage, do the backend students all know the word Session, translated is the Session. SessionStorage is a concept at the front end. It can only save some data in the current session, refresh the page data still exists. But after the page closes, the data in sessionStorage will be cleared.

Cookie compares with webStorage

  1. Cookies of different data life cycles are generally generated by the server and the expiration time can be set. If cookies are generated on the browser side, they will be disabled by default. LocalStorage is permanently saved unless permanently removed. SessionStorage is valid only for the current session and is cleared after closing the page or browser
  2. Cookie – generally 4KB localStorage and sessionStorage generally 5M or larger
  3. Different from the server, cookies carry HTTP headers each time. If too much data is stored using cookies, performance problems may occur. LocalStorage and sessionStorage are stored only in the client (browser) and do not participate in the communication with the server.
  4. Different scoped sessionstorages are not shared in different browser Windows, even on the same page; Localstorage is shared in all origin Windows; That is, as long as the browser is not closed, the data remains cookie: also shared across all the same origin Windows. This means that as long as the browser is not closed, the data still exists

session

Is a data structure stored on the server to track user status. this data can be stored in clusters, databases, files

Cookie and Session comparison:

  • Cookie data is stored on the client’s browser and session data is stored on the server
  • Cookies are not very secure. Others can analyze cookies stored locally and cheat cookies. For security, session should be used. Sessions are commonly used for user authentication
  • The session is stored on the server and the client does not know the information in it; Instead, cookies are stored on the client side, and the server can see the information in them
  • Sessions are stored on the server for a certain period of time. When the number of accesses increases, the performance of your server will be affected. In order to reduce the performance of your server, you should use cookies
  • The session holds objects, and the cookie holds strings
  • Session cannot distinguish paths. During the same user’s visit to a website, all sessions can be accessed anywhere. However, if the path parameter is set in cookies, cookies under different paths in the same website cannot be accessed to each other
  • Cookie: it is a mechanism for the client to save user information and record some user information. It is also a way to realize the Session.

* Image analogy ~~~~*

The communication between the client and the server can be simply understood as follows: for example, when you think a lecturer in a technology sharing salon has spoken well, you ask him a few questions after the meeting, and he answers your questions. This is a conversation. But the lecturer was so popular that the staff collected the questions and gave each questioner a number plate. The lecturer gave corresponding answers according to the number plate and told the corresponding people. So that’s Session. Some time later, when you run into the lecturer again, he finds that you have the same answer he gave you last time and knows that you are a studious programmer. And you’re like, wow, the instructor recognizes me, and this is Cookie, your little dessert. The client is the technology enthusiast listening to the lecture, and the server is the lecturer.