Why is therecookie

Web applications use the HTTP protocol to transfer data. The HTTP protocol is stateless. Once the data exchange is complete, the connection between the client and the server is closed and a new connection needs to be established to exchange data again. This means that the server cannot track sessions from the connection.

You may have logged into a website and been reminded to remember your account and password so that you don’t have to enter it again the next time you visit. This is the function of cookie. When we visit again, it is convenient for the server to directly fetch the things we fetched last time according to our cookie (for each cookie, the server will store the data we fetched last time to this cookie, and the next time for the same cookie, it will fetch directly here).

What is theCookie

Cookie is generated by the server side and sent to the User-Agent (usually the browser). (The server tells the browser to set the Cookie), and the browser will automatically save the Cookie to a text file in a directory with key/value. This Cookie is also automatically sent to the server the next time the same website is requested, that is, added to the request header (if the browser is set to enable cookies). Cookie is a small file (the browser has a limit on the memory size of the Cookie ——- to record some information)

CookieThe characteristics of

CookieHas a shelf life

Each browser has its own cookie. When each request is made, the corresponding cookie will be sent according to the domain. Save date can be set by setting Expires and Max-Age, otherwise the default is temporary storage, that is, the browser will be closed and disappear.

Document. cookie = 'expires= time /max-age= seconds'

Satisfy the same origin policy

Although the website images.google.com and www.google.com belong to Google, they have different domain names and cannot manipulate each other’s cookies. And PATH must also be the same to access each other’s cookies, need to note that different browsers on PATH access regulations are different, for Chrome, PATH must be the current directory, set to other directories invalid, only the current page can only access the current directory and cookies above

CookieMemory size is limited

Cookies are limited in number and size, usually 4K

CookieThe safety of

Cookies can be changed locally. File-sensitive data should not be placed in cookies

CookiesThe use of

Cookies are actually mainly set by Web server developers. Front-end developers rarely use cookies, but they also use them, for example, to set account information for the login page. Let’s open the browser console. For Chrome, switch to Application, and you can see the Cookie item under Storage on the left. Click on it to see the current cookies, such as the following

You can see that each line is a cookie, which contains its value and its related information. We will discuss the meaning of each attribute in more detail in a moment. So let’s just say how do I set a Cookie

document.cookie='name=xiaoming; expires='+oDate

It simply sets a Cookie and uses’ for each property; ‘, and cannot set more than one Cookie at a time, can only set one Cookie at a time, the later Cookie does not overwrite the previous Cookie, will only be concatenated to the current Cookie string. The specific use method is as follows.

  • Set the cookie through document.cookie

    • Format: Name = Value (document.cookie = 'age =18; max-age=1000')
    • Will not overwrite (note the SettingscookieFrom time to time. You cannot set more than one at a timecookieYou can only set one at a timecookieIf the same field is set later, if the same field is set, the samepath, then the back will overwrite the front, otherwise a new one will be addedcookie. Set it topathLater, thecookieCan only be thepathAnd the following directories, such as I Settingspath=/webSo when I’m in/this directory I can’t access what I set to/webthecookieAnd I was in/web/xxxIt can be accessed in this directory.
    • Expiration time:Expires = time /max-age= seconds(default is temporary if not set)
  • readcookie(Note that it is usually only when we containhttpSet when requestedcookieTo be effective, different browsers are not the same, such as IE can be set directly), through the string segmentation.
  • deletecookie: Expired (even if the date is set, ensure that the date is less than the current date)