Cookie

Cookies are a local storage mechanism supported by browsers. Typically generated by server Settings, it is automatically stored in the browser in response to a request. Is a very small text file placed on your hard drive by a web server. It essentially as your proof of identity, it can only be used by you and can only be read by providing server, each time you visit the web site browser the website of cookies will be sent back to the web server, web site at the same time can also change your machine corresponding cookies, Cookie name and value can be defined by the server-side development itself.

use

One use is to store a user’s password and ID on a particular web site. Generally used for site personalization and site tracking.

Cookies are simply a web tracking statistic used to better understand usage patterns and improve the effectiveness of web visitors.

If the server needs to record user status, the server includes a set-cookie response header in the response message, and the client stores the Cookie information against this response header. When requesting the server again, the client will include a Cookie request header in the request information, and the server will check the user identity and status according to this request header.

Cookie in practice

For example, when the submit button is pressed, the Cookie value will be brought back by the background after processing the request and jumping to the corresponding page, or when the back-end request returns. After the server receives the Cookie data, it can use this data to determine what is returned to the foreground (and further, in conjunction with the Session). If the user of the site has not visited it, a new ID is created for the address and some key-value pairs are sent to the foreground. These values are put into the Header in the response and sent to the browser through the set-cookie Header. The browser then has the value of the Cookie.

Take the browsing history of a treasure for example

One of these computers is telling you that you’ve been browsing for certain items, and the other computer might be browsing for other items, because different cookies correspond to different browsing records, which seems a little illogical.

So used the account login, as long as the login can put all records with the background database account strong association, can obtain a complete browsing record.

The connection between cookies and sessions

The server uses session to save the encrypted state of the user, and then the client uses cookie to save the session. The server plants the session into cookie, and then the cookie will carry the session when it is accessed next time. Then achieve the effect of an identity authentication.

Token

Token is a string of strings generated by the server and placed in the session as a Token for the client to request. After the first login, the server generates a Token and returns the Token to the client. In the future, the client only needs to bring the Token to request data. No need to bring a username and password again.

use

Tokens prevent forms from being submitted repeatedly. Typically, when the client initializes the page, it calls the back-end code, which generates a Token, returns it to the client, and stores the Token. (You can store the Token in the foreground using a hidden field in the Form. Request tokens can also be stored in cookies or Local Storage), and then compare the tokens in the request with those in the session:

Session applications are relatively secure, but cumbersome. In the case of multiple pages and multiple requests, multiple tokens must be generated at the same time, which consumes more resources and reduces the execution efficiency. Therefore, a cookie can be used to store authentication information instead of session tokens.

LocalStorage and sessionStorage

In HTML5, a new localStorage feature is added, which is mainly used as localStorage. In order to solve the problem of insufficient cookie storage space, the name can be seen that the former is always local, and the latter is accompanied by session, once the window is closed, it will disappear. Different browsers cannot share information in localStorage or sessionStorage. Different pages of the same browser can share the same localStorage (pages belong to the same domain name and port), but different pages or tabs cannot share the sessionStorage information.

use

LocalStorage is equivalent to a front-end database, so you can add, delete, change and check

Supported types the localStorage in most browsers supports only string storage. The localStorage automatically converts the localStorage to a string

Add: Cookies are no longer recommended for storage. LocalStorage and sessionStorage can be used if you don’t have extensive data storage requirements. Try to use localStorage for data that doesn’t change much, otherwise use sessionStorage, okay

  • Male _ no. ❤; Front-end honest person, you can exchange and learn with small partners!