Uncovering the secrets of cookies: Where do they really exist?

WBOY
Release: 2024-01-19 09:06:02
Original
1223 people have browsed it

Uncovering the secrets of cookies: Where do they really exist?

Uncover the secrets of cookies: Where do they exist?

In daily website use, we often deal with cookies, such as remembering passwords, shopping carts, advertising recommendations, etc., and the role of cookies cannot be ignored. So, what exactly are cookies? Where are they stored? Let’s uncover the secrets of cookies today.

What are cookies?

A cookie is a small data file that is stored on the user's computer and is typically sent by a website to a browser, which then sends it back to the appropriate server on subsequent requests. They are usually used to store the user's session information, shopping cart contents, preference settings, etc., so that the next time they visit the website, the user can be automatically recognized and the corresponding data loaded.

Where does it exist?

When talking about the location where cookies are stored, we need to first understand the basic knowledge of the HTTP protocol. In the HTTP protocol, there are two different storage methods: session storage and persistent storage.

  1. Session Storage

Session storage means that when the user closes the browser window, all cookies will be deleted from the computer. This means that the session cookie will only survive the user's current session. If the user closes the browser, the session cookie is deleted.

The following is a simple example that demonstrates how to create a session cookie using the Express framework:

const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()

app.use(cookieParser())

app.get('/', (req, res) => {
  res.cookie('username', 'john doe', {
    httpOnly: true
  })
  res.send(`Hello World!`)
})

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000`)
})
Copy after login

In this example, we use the cookie-parser middleware , stores the session cookie in browser memory. If the user closes the browser window, the cookie will be deleted.

  1. persistent storage

Persistent cookies are different from session cookies in that they can have an expiration time set, and they will still be stored on your computer until the expiration time even if the browser is closed. arrived or manually deleted.

Here is an example that demonstrates how to create a persistent cookie using the Express framework:

const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()

app.use(cookieParser())

app.get('/', (req, res) => {
  res.cookie('username', 'john doe', {
    maxAge: 60 * 60 * 1000,
    httpOnly: true
  })
  res.send(`Hello World!`)
})

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000`)
})
Copy after login

In this example, we set the maxAge option to 1 hour, which Meaning the cookie will expire after 1 hour. When the expiration time is reached, it will be automatically deleted from the computer.

Conclusion

Through the above introduction, we can know that the browser can store session cookies and persistent cookies. Session cookies are stored in the browser's memory, while persistent cookies are stored on the user's computer's hard drive. Persistent cookies will also be deleted once the expiration time is reached or manually deleted by the user. Therefore, in application design, the type and life cycle of cookies need to be carefully considered to ensure the normal operation of the application.

The above is the detailed content of Uncovering the secrets of cookies: Where do they really exist?. 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!