Home > Web Front-end > JS Tutorial > body text

Detailed explanation of cookie usage

韦小宝
Release: 2018-03-12 10:36:26
Original
6449 people have browsed it

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.

1. What is a cookie?

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

Open Firefox, right-click

Detailed explanation of cookie usage

and select "View page information"

Detailed explanation of cookie usage

Select the security option and click "View cookies"

Detailed explanation of cookie usage

The series of folders shown in the picture above contain a series of related information for entering each website. Detailed explanation of cookie usage

Click on the folder and you will find the information as shown in the picture above. Each piece of information is a cookie, and they all have a name, content and expiration time. It is important to emphasize here that the expiration time means that once this time is reached, the cookie information will automatically disappear.


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. Detailed explanation of cookie usage

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.

After logging in, the page is as follows:

Detailed explanation of cookie usage

If we launch and enter this page, you will find that there is no need to log in, and you will directly enter the interface shown in Figure 3. However, if you remove all cookies again, the page shown in Figure 2 will appear when you log in to this interface again.

Compared to this point, everyone has a preliminary understanding of the usage of cookies.

2. Cookie programming

1. Setting cookie

Detailed explanation of cookie usage

The effect is as follows:

Detailed explanation of cookie usage

View cookies (the first folder loads these cookie information)

Detailed explanation of cookie usage

Pay attention to the name and content. "a=hello" means the name before the equal sign, and the content after the equal sign.

**Cookies created through the above method are relatively cumbersome, and it will be troublesome if you write a few more entries. We can create cookies through functions.

Detailed explanation of cookie usage

2. Obtain cookie


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: Detailed explanation of cookie usage
is a long string

. What we need to do now is to obtain a single piece of cookie information and output the value after the equal sign. Here’s how to do it: ###

Detailed explanation of cookie usage
The output results are as follows:
Detailed explanation of cookie usage
1Detailed explanation of cookie usage
**Why alert(getCookie("eyes"));alert(getCookie("hair")) ;Why do these two codes appear empty when they are run?

1Detailed explanation of cookie usage
Why add a space?

1Detailed explanation of cookie usage
As shown in the figure above, when splitting a string through split(), you should write like this: split("; ");

3. Remove cookie

As long as the cookie expiration time is set to a negative value, the cookie will disappear directly.

1Detailed explanation of cookie usage

1Detailed explanation of cookie usage

##3. All codes are as follows:

1Detailed explanation of cookie usage

1Detailed explanation of cookie usage

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 the

basic 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!