In our browsers, we often involve the exchange of data, such as when you log in to your email or log in to a page. We often set up remember me for 30 days, or auto-login options at this time. So how do they record information? The answer is today’s protagonist cookie. Cookies are set by the HTTP server and stored in the browser. However, the HTTP protocol is a stateless protocol. After the data exchange is completed, the server and The client's link will be closed, and a new link will need to be established each time data is exchanged.
When you enter a website, you need to log in with a password and account. After you register for the first time, log in and enter the main page of the website. At this time, your computer will automatically save a cookie information (this information can be viewed through Firefox Browser, chrom does not have this function). This information contains your first login account and password. When you The next time you log in to this website, the browser will automatically retrieve the relevant cookie information from your computer, so that the login page will not appear but will jump directly to the homepage of the website.
Take Sina Weibo as an example
1. The following shows the operation of viewing cookie information
The series of folders shown in the picture above contain a series of related information for entering each website.
2. Take “Sina Weibo” as an example to observe the creation and disappearance of cookie information.
First, I click on Sina’s “Remove All” (this function is at the bottom of the picture above) to eliminate all cookies. cookie information. Then enter the Sina Weibo page.
The status of not logged in is displayed at this time. Clear all cookie information again (these irrelevant cookie information will appear when you log in to the website) to avoid affecting subsequent demonstrations.
View cookies (the first folder loads these cookie information)
Pay attention to the name and content. "a=hello" means the name before the equal sign, and the content after the equal sign.
Now that the cookie has been created, what program will the browser use to obtain any information in the computer when we log in to the page? A cookie message.
The effect we see now through alert(document.cookie); is as follows:
is a long string
The output results are as follows:
**Why alert(getCookie("eyes"));alert(getCookie("hair")) ;Why do these two codes appear empty when they are run?
Why add a space?
As shown in the figure above, when splitting a string through split(), you should write like this: split("; ");
As long as the cookie expiration time is set to a negative value, the cookie will disappear directly.
##3. All codes are as follows: Let’s take the cookie above as an example. If you want to master cookies, you must understand its origin and what it is. The purpose of its existence is to save user information, and then know how to save it and the form of saving it. , we can use the basic knowledge and syntax of js we learned to operate it and change it to make it what we need. After understanding the requirements, in the final analysis, we still need to examine thebasic operations of js, string Operations, application of regular expressions, etc.
More articles about cookies:
How to operate cookies using pure JS
AngularJs operating Cookies example sharing
php session control session, cookie introduction
The above is the detailed content of Detailed explanation of cookie usage. For more information, please follow other related articles on the PHP Chinese website!