Current Location: Home> Latest Articles> Exploring the Storage Location of Cookies: Where Are They Stored?

Exploring the Storage Location of Cookies: Where Are They Stored?

M66 2025-07-01

Cookie Storage Locations

With the development of the internet, websites store some data in the browser during user browsing, enabling faster loading on subsequent visits. This data storage mechanism is known as cookies. So, where exactly are cookies stored? We will explore the storage locations of cookies in different browsers.

Browsers store cookies on the client-side, and the specific storage location depends on how the browser is implemented. Here are the cookie storage locations for some popular browsers:

  • Chrome: Cookies are stored in the %AppData%LocalGoogleChromeUser DataDefaultCookies file on the user's computer.
  • Firefox: Cookies are stored in the %AppData%RoamingMozillaFirefoxProfilesXXXX.defaultcookies.sqlite file on the user's computer.
  • Safari: Cookies are stored in the ~/Library/Cookies/cookies.binarycookies file on the user's computer.
  • Edge: Cookies are stored in the %AppData%LocalMicrosoftEdgeUser DataDefaultCookies file.
  • Internet Explorer: Cookies are stored in the C:UsersUsernameAppDataRoamingMicrosoftWindowsCookiesLow file. Note that this method is gradually deprecated in Windows 10 and later versions.

Overall, cookies are generally stored in local files, not on remote servers.

Cookie Operations in JavaScript

In JavaScript, cookie operations are mainly done using the document.cookie property. Below are examples of how to read, add, and delete cookies in JavaScript.

Reading Cookie Values

To read a cookie in JavaScript, you can use the following function:

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

This function uses a regular expression to match the specified cookie name and returns the corresponding value. Note that since cookie values may contain non-ASCII characters, such as Chinese, they need to be decoded using decodeURIComponent().

Adding Cookies

To add a cookie in JavaScript, you can use the following code:

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=/";
}

This function accepts three parameters: name, which represents the cookie's name; value, which represents the cookie's value; and duration, which represents the cookie's lifespan (in days). It calculates the expiration time and stores the cookie in document.cookie.

Deleting Cookies

To delete a cookie in JavaScript, you can use the following code:

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=/";
  }
}

This function sets the cookie's expiration time to a past date, making it expire immediately.

Cookie Operations in Node.js

In Node.js, we can use third-party libraries to manipulate cookies. In this example, we will use the cookie-parser library to add, read, and delete cookies.

Installing cookie-parser

To install cookie-parser, run the following command in the terminal:

npm install cookie-parser

Adding Cookies

To add a cookie in Node.js, use the following code:

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');
});

This code sets a cookie named 'name' with the value 'value', an expiration time of 900000 milliseconds, and an httpOnly flag set to true.

Reading Cookies

To read a cookie in Node.js, use the following code:

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

Deleting Cookies

To delete a cookie in Node.js, use the following code:

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

Conclusion

This article covered the storage locations of cookies as well as how to manipulate cookies in JavaScript and Node.js. It is important to note that cookies are not encrypted, so sensitive information should not be stored directly in cookies. Also, be sure to properly set the cookie's expiration time to avoid security risks caused by expired data.