First, let's briefly introduce the cookie. Simply put, the cookie is the information that the server temporarily stores on our computer. When we browse the website, the Web server will first send a small piece of information to the computer, and the cookie will record the text, behavior, or some choices we make on the website. When you access the same website again next time, the Web server first looks
What is cookie?
First, let's briefly introduce the cookie. Simply put, the cookie is the information that the server temporarily stores on our computer. When we browse the website, the Web server will first send a small piece of information to the computer, and the cookie will record the text, behavior, or some choices we make on the website. When you visit the same website next time, the Web server will first check whether there is any cookie information it left. If so, the Web server will judge the user based on the content in the cookie, send specific webpage content. Currently, cookie is most widely used to record user login information, so that you do not need to enter your username or password for the next visit, which is more convenient.
For more information about cookies, see Baidu encyclopedia http://baike.baidu.com/view/835.htm.
Cookie features
Cookie has four attributes: cookie lifecycle, path attribute, domain attribute, and cookie Security attribute.
Ookie lifecycle: expires. By default, the cookie only exists in the browser session period. After the browser is closed, the cookie does not exist. We can set the expiration time for the cookie through expires, after the time expires, the cookie will disappear and the cookie file will be automatically deleted.
Path attribute: by default, the cookie can be called in the same directory file. For example.
Domain attribute: If the domain is set to ".beyondweb.cn", all files on the server under ".beyondweb.cn" can call the same cookie.
In addition, cookies have some limitations. A browser can store up to 300 cookies. A single web server can only store up to 20 cookies, and each cookie cannot exceed 4000 bytes.
Create cookie in JS
We can use document. cookie to create a cookie.
1234567891011121314 |
New document Document. cookie = "user = wangchi"; document. cookie = "pass = 1234" alert (document. cookie); script |
Note: Some browsers need to create cookies in the server environment. You can use Firefox first.
We can run the above Code through the Firefox browser.
Then, right click → view page information → Security → view cookie so that we can see the cookie we saved, as shown in
However, when we restart the browser, the cookie disappears. This is because, by default, the browser closes and the cookie disappears. Next we will set its expiration time through expires.