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:
Overall, cookies are generally stored in local files, not on remote servers.
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.
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().
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.
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.
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.
To install cookie-parser, run the following command in the terminal:
npm install cookie-parser
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.
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);
});
To delete a cookie in Node.js, use the following code:
app.get('/clearCookie', function(req, res) {
res.clearCookie('name');
res.send('cookie cleared');
});
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.