What is a session?
Session refers to the data exchange between the browser and the server. The white is the request and response of the browser and the server.
http protocol is stateless, there is no correlation between requests
cookie and session role? Gansha?
Use cookie and session may be a session to maintain , to keep the session refers to: maintain the user's login status , the server records the user's login information, the next time the user accesses the server to recognize the
About cookie
A cookie is a container storing data browser, the size is 4k
Data in the cookie will be at the time of request, to carry
The default is session-level cookie, once you close the browser cookie will be destroyed
sessionid cookie can be stored inside
Some common interview questions
1 cookie is a browser container to store data, why the server can get the data in a cookie?
Because the request to the server when the cookie, the cookie data will be carried in the request header ; server can read the contents of the request header, and indirectly obtain the data in the cookie. (Data in the cookie request header is sent to the server)
2 cookie is a browser container to store data, why the server can set the data in the cookie?
Server is not directly modify a cookie, but you can set up response; setcookie response header is set; setcookie: 'value', set in the future tells the browser cookie.
About session
session server is a container for storing data, each user has its own session space
session mechanism
01 If the user is accessing the server for the first time, will automatically generate a sessionid, this sessionid is a random string;
02 According sessionid, the server will open a session of storage space for the user (will really create a session file, the name of the file is sessionid, can be used to store data)
03 response header, to pass sessionid back to the browser, set in a cookie
04 second visit, the data stored inside the cookie will be carried in the request header in the request, the sessionid carrying
05 According sessionid server session can be found corresponding to the file, the user will know this.
sessionStorage
Storing container data in a browser, 5M
Session level, a close browser destruction
Can not be shared between multiple windows
Do not carry
localStorage
Browser container storing data, 5M IE only about 1M
Permanent storage, as long as the user does not manually deleted, there has been
Multi-window can be shared
Do not carry
<Script>
. 1 . The localStorage API
( . 1 ) is provided: localStorage.setItem (Key, value)
( 2 ) Get: localStorage.getItem (Key)
( . 3 ) Delete: localStorage.removeItem (Key)
( . 4 ) Clear all: localStorage .clear () with caution, will remove all
Note:
01 localStorage can store the string data type, the data is read out of the string
02 localStorage storing complex data types
( 1) storage, obj => jsonStr convert the object there json string format into
the JSON.stringify (obj / ARRY) turn into the complex data type formatted string json
localStorage.setItem (key, value) is stored
( 2) obtaining, jsonStr => obj
localStorage.getItem (key)
acquired is json strings need to convert the array or object js
the JSON.parse (jsonStr)
</ Script>