Table of Contents
1. What is a cookie?
3. Remove cookie
Home Web Front-end JS Tutorial Detailed explanation of cookie usage

Detailed explanation of cookie usage

Mar 12, 2018 am 10:36 AM
cookie ie explain

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

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("; ");

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

Does clearing cookies have any impact? Does clearing cookies have any impact? Sep 20, 2023 pm 06:01 PM

The effects of clearing cookies include resetting personalization settings and preferences, affecting ad experience, and destroying login status and password remembering functions. Detailed introduction: 1. Reset personalized settings and preferences. If cookies are cleared, the shopping cart will be reset to empty and products need to be re-added. Clearing cookies will also cause the login status on social media platforms to be lost, requiring re-adding. Enter your username and password; 2. It affects the advertising experience. If cookies are cleared, the website will not be able to understand our interests and preferences, and will display irrelevant ads, etc.

What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) Feb 10, 2024 am 10:30 AM

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

What are the dangers of cookie leakage? What are the dangers of cookie leakage? Sep 20, 2023 pm 05:53 PM

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

See all articles