Home Backend Development PHP Tutorial Discover the location of cookies: Where exactly are they stored?

Discover the location of cookies: Where exactly are they stored?

Jan 19, 2024 am 09:53 AM
cookie explore Storage location

Discover the location of cookies: Where exactly are they stored?

With the continuous development of Internet technology, it is common for us to browse information, shop, pay and other behaviors on the website. In order to facilitate users to browse the website, the website server will save some data on the user's browser. The next time the user visits the website, the data will be extracted for the server to use. One such mechanism for saving data is cookies. So, where are cookies stored? This article will discuss where cookies are stored and provide some code examples.

  1. Cookie storage location

The browser saves cookies on the client side, so the storage location of cookies is determined by the specific browser implementation. Different browsers have different cookie storage locations. In mainstream browsers, the location where cookies are stored is as follows:

  • Chrome: cookie information is stored in the %AppData%LocalGoogleChromeUser DataDefaultCookies file on the user's computer;
  • Firefox: cookie information Saved in %AppData%RoamingMozillaFirefoxProfilesXXXX.defaultcookies.sqlite on the user's computer;
  • Safari: Cookie information is saved in ~/Library/Cookies/cookies.binarycookies on the user's computer;
  • Edge: Cookie information is stored in %AppData%LocalMicrosoftEdgeUser DataDefaultCookies;
  • Internet Explorer: Cookie information is stored in C:Users usernameAppDataRoamingMicrosoftWindowsCookiesLow, but it has been gradually deprecated in systems after Windows 10.

In short, in most cases, cookie information is saved in local files, not on the remote server.

  1. Cookie operation in JavaScript

Below we will introduce how to operate Cookie in JavaScript. Generally speaking, reading, adding, and deleting cookies are based on the document.cookie attribute.

Read the cookie value:

In JavaScript, you can read the cookie by:

function getCookie(name){
  var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  if(arr=document.cookie.match(reg)){
    return decodeURIComponent(arr[2]);
  }else{
    return null;
  }
}
Copy after login

Use a regular expression to match the name of the cookie you are looking for, and then If a match is found, the value corresponding to the name is returned. It should be noted that since the cookie value may contain non-ASCII characters such as Chinese, it needs to be decoded (using the decodeURIComponent() method).

Add cookies:

In JavaScript, you can add cookies in the following ways:

function setCookie(name,value,duration){
  var exp = new Date();
  exp.setTime(exp.getTime() + duration * 24 * 60 * 60 * 1000);
  document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
Copy after login

Among them, name represents the name of the cookie, value represents the value of the cookie, and duration represents The validity period of the cookie (in days). A Date object is used here to calculate the expiration time of the cookie, and then the cookie information is stored in document.cookie.

Delete cookies:

In JavaScript, you can delete cookies in the following ways:

function deleteCookie(name){
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if(cval != null){
      document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
  }
}
Copy after login

Among them, name represents the name of the cookie. Here, the cookie's expiration time is set to a past time, and then stored in document.cookie.

  1. Cookie operations in Node.js

In Node.js, you can use third-party libraries to conveniently operate cookies. Here we take the cookie-parser library as an example to introduce how to add, read, and delete cookies in Node.js.

Install cookie-parser:

Enter the following command in the terminal to install cookie-parser:

npm install cookie-parser
Copy after login

Add cookie:

In Node.js , you can add cookies in the following ways:

const cookieParser = require('cookie-parser');
app.use(cookieParser());
app.get('/setCookie',function(req,res){
  res.cookie('name','value',{maxAge: 900000, httpOnly: true });
  res.send('cookie added');
});
Copy after login

Among them, name represents the name of the cookie, value represents the value of the cookie, and maxAge represents the validity period of the cookie (in milliseconds).

Reading cookies:

In Node.js, you can read cookies in the following ways:

app.get('/getCookie',function(req,res){
  var value=req.cookies.name;
  res.send('cookie value:'+value);
});
Copy after login

Among them, name represents the name of the cookie.

Delete cookies:

In Node.js, you can delete cookies in the following ways:

app.get('/clearCookie',function(req,res){
  res.clearCookie('name');
  res.send('cookie cleared');
});
Copy after login

Among them, name represents the name of the cookie.

  1. Conclusion

This article introduces the discussion of cookie storage location and cookie operation methods in JavaScript and Node.js. It should be noted that cookie information is not encrypted, so sensitive data should not be stored directly in cookies, but should be encrypted. In addition, you need to pay attention to the validity period of cookies to avoid security issues caused by expired cookies.

The above is the detailed content of Discover the location of cookies: Where exactly are they stored?. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Revealing the secrets of canvas properties Revealing the secrets of canvas properties Jan 17, 2024 am 10:08 AM

To explore the secrets of the canvas attribute, you need specific code examples. Canvas is a very powerful graphics drawing tool in HTML5. Through it, we can easily draw complex graphics, dynamic effects, games, etc. in web pages. However, in order to use it, we must be familiar with the related properties and methods of Canvas and master how to use them. In this article, we will explore some of the core properties of Canvas and provide specific code examples to help readers better understand how these properties should be used.

Explore the future development trends of Go language Explore the future development trends of Go language Mar 24, 2024 pm 01:42 PM

Title: Exploring the future development trends of Go language With the rapid development of Internet technology, programming languages ​​are also constantly evolving and improving. Among them, as an open source programming language developed by Google, Go language (Golang) is highly sought after for its simplicity, efficiency and concurrency features. As more and more companies and developers begin to adopt Go language to build applications, the future development trend of Go language has attracted much attention. 1. Characteristics and advantages of Go language Go language is a statically typed programming language with garbage collection mechanism and

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.

Frequently Asked Questions and Solutions about Cookie Settings Frequently Asked Questions and Solutions about Cookie Settings Jan 19, 2024 am 09:08 AM

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered

Exploration of commonly used database selections in Go language Exploration of commonly used database selections in Go language Jan 28, 2024 am 08:04 AM

Explore commonly used database selections in Go language Introduction: In modern software development, whether it is web applications, mobile applications or Internet of Things applications, data storage and query are inseparable. In the Go language, we have many excellent database options. This article will explore commonly used database choices in the Go language and provide specific code examples to help readers understand and choose a database that suits their needs. 1. SQL database MySQL MySQL is a popular open source relational database management system. It supports a wide range of features and

Exploring Graph Programming in Go: Possibilities of Implementing Graph APIs Exploring Graph Programming in Go: Possibilities of Implementing Graph APIs Mar 25, 2024 am 11:03 AM

Exploring graphics programming in Go language: the possibility of implementing graphics APIs With the continuous development of computer technology, graphics programming has become an important application field in computer science. Through graphics programming, we can realize various exquisite graphical interfaces, animation effects and data visualization, providing users with a more intuitive and friendly interactive experience. With the rapid development of Go language in recent years, more and more developers have begun to turn their attention to the application of Go language in the field of graphics programming. In this article, we will explore implementing

How to find cookies in your browser How to find cookies in your browser Jan 19, 2024 am 09:46 AM

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

An in-depth exploration of the Linux kernel source code distribution An in-depth exploration of the Linux kernel source code distribution Mar 15, 2024 am 10:21 AM

This is a 1500-word article that explores the Linux kernel source code distribution in depth. Due to limited space, we will focus on the organizational structure of the Linux kernel source code and provide some specific code examples to help readers better understand. The Linux kernel is an open source operating system kernel whose source code is hosted on GitHub. The entire Linux kernel source code distribution is very large, containing hundreds of thousands of lines of code, involving multiple different subsystems and modules. To gain a deeper understanding of the Linux kernel source code

See all articles