Briefly understanding session, cookie, sessionStorage, localStorage of

A, cookie and session

  First session cookie and the browser for client and server data exchange, tracking the browser user identity through the session.

1、cookie

  (1), generally generated by the server, expiration time may be provided, if the browser generates a default browser is closed after the failure;

  (2), in communication with the server, each request in http request header;

  (3), the data stored in memory, the data size of 4KB, stored in a string type;

2、session

  (1), the service receives a request sent by the client, need to create a session object, then checks whether the request contains the client sessionID, create server-side session object based on the id, if the client is not given, then the server creates a new session, sessionID will be returned to the client, this id will be saved in a cookie in the client;

  (2), if the user disables cookie, you will have to use URL rewriting browser does not support cookie time, will rewrite the URL address to access sessionID after stitching;

  (3), for storage by the HashTable similar data structure, there is no size limit;

  Compared security session cookie in terms of better, seesionID stored in a cookie, you need to break to break the session cookie; and sessionID is encrypted, sessionID is landing or someone will have to start session_start; after the second start session_start before the sessionID will fail, so the session better security.

Two, WebStorage

  HTML5's WebStorage provides two API: localStorage (local storage) and sessionStorage (session storage)

1, Life Cycle:

  localStorage life cycle is permanent, after closing the page or browser data in localStorage will not disappear. localStorage unless the initiative to remove data, otherwise the data will never disappear.
  sessionStorage life cycle is only valid in the current session. sessionStorage introduces the concept of a "browser window", sessionStorage is homologous window is always present in the data. As long as the browser window is not closed, even if refresh the page or go to another page homology, the data still exists. But sessionStorage will be destroyed after you close the browser window. Meanwhile a separate window open with the same page, sessionStorage is not the same.

2, storage size:

  And storing the data size localStorage sessionStorage are generally: 5MB

3, the storage location:

  localStorage and sessionStorage are stored in the client, not interactive communication with the server.

4, storage type:  

  sessionStorage localStorage and only store string type, for complex objects can be used to provide the ECMAScript JSON object stringify to handle and parse

Since WebStorage not transferred to the server, so the more secure, but also save network traffic, data storage is also larger than the cookie, stored locally but also makes the read data faster, because there are counterfeit problem, it is not recommended to store sensitive information

Daily One problem:

  var, let, const difference?

  • var and let to declare variables, const for declaring a constant read-only;
  • Var variable declaration, the absence of block-level scope, are effective in the global scope, and the let const statement, it is only valid for the code block;
  • let var and const does not exist like that kind of  "variable lift"  phenomenon, so the var defined variables can first use, after the statement, but let const and can only be declared after use;
  • Variables let declare the existence of a temporary dead zone, that is, as long as there let block-level scope, the variable declared it to bind to this region, no longer subject to external influences;
  • let not allowed in the same scope, the statement repeated the same variable;
  • const must be initialized at the time of the assignment statement, once declared, the value of its declaration is not allowed to change, but does not allow duplicate statement;

Guess you like

Origin www.cnblogs.com/tg666/p/11355465.html