To talk about a cookie

  • cookie:

  • – HTTP is a stateless protocol. HTTP itself does not store the communication between a response and a request

  • – Cookie’s job is not to store locally, but to maintain state

  • – Cookie is a means to bypass the STATeless protocol of HTTP. The server can maintain the state between the client and server through the cookie information carried by the user’s request

  • Cookie features:

  • – The memory provided by cookies is small and limited

  • – Cookies must be sent along with the request, too many cookies will have more performance cost

  • – Cookie is in plaintext during HTTP transmission and has low security, except HTTPS

Talk about the session

  • Server storage
  • 1. The client sends a login request with login information in the packet
  • 2. The server receives the request, creates a session object, and then creates a key-value for the current user information that will never be repeated
  • 3. The server returns a response, and the cookie carries the sessionID
  • 4. The client receives the response and saves the sessionID in a cookie
  • 5. When the client sends a request for the second time, the cookie automatically carries the sessionID
  • 6. The server obtains the sessionID in the cookie requested by the client through parsing to determine whether the user information is saved in the session object
  • 7. If the authentication succeeds, the server responds to the request. Otherwise, you need to log in again

To talk about a Storage

  • LocalStorage: localStorage
  • - Save the data on the local disk by browser and domain nameCopy the code
  • - The stored data exists for a long time, and the data can be read each timeCopy the code
  • - the size is about 5MCopy the code
  • - The client does not communicate with the serverCopy the code
  • - Communication across pagesCopy the code
  • - Interface encapsulation is very niceCopy the code
  • Interface:Copy the code
  • Setting: localStorage. SetItem (key, value)Copy the code
  • Access: localStorage. The getItem (key)Copy the code
  • Delete: localStorage. RemoveItem (key)Copy the code
  • Temporary storage interface :(the page disappears when closed and cannot communicate across pages)
  • Setting: sessionStorage. SetItem (key, value)Copy the code
  • Access: sessionStorage. The getItem (key)Copy the code
  • Delete: sessionStorage. RemoveItem (key)Copy the code