The Wayback Machine - https://web.archive.org/web/20240911120712/https://www.geeksforgeeks.org/explain-persistent-cookie/
Open In App

Explain Persistent Cookie

Last Updated : 19 Sep, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A Persistent Cookie is a small piece of data that is stored on a user’s computer and remains there after the user has closed their browser. They can be used to store user preferences and other information. Persistent cookies are used to improve the user experience by allowing website owners to store information that can be used to customize a user’s experience on a website. There are several characteristics of Persistent Cookie which are described below:

  • A Persistent Cookie is also known as a stored cookie, which is a small piece of data that is stored on a user’s computer by a web browser. It is stored on a user’s computer, as well, even after the web browser is closed. Even the Persistent Cookie can be used to store information that needs to be persistent across user sessions, such as a user’s preferences or login information. It is typically used in conjunction with a web server.
  • When a user visits a website, the web server sends the persistent cookie to the web browser & these cookies are small text files that a web server stores on a user’s computer. The file is saved in a location that is specific to the web browser. Each time the user loads the website, the browser sends the cookie back to the server to notify the website of the user’s previous activity.
  • They can also be used for marketing purposes, such as targeted advertising.
  • Some persistent cookies are set to expire after a certain amount of time, while others remain on the user’s computer indefinitely. So, the length of time can range from a few days to several years. They are typically stored on the user’s hard drive for a period of time that is determined by the website.
  • Most persistent cookies are “first-party” cookies, which means they are set by the website that the visitor is visiting. 
  • Some persistent cookies are “third-party” cookies, which means they are set by a website other than the one the visitor is visiting. 
  • However, persistent cookies can also be used for malicious purposes, such as tracking a user’s activity without their knowledge or consent.

Information provided by the persistent cookies may be shared with third-party organizations, such as advertisers and data brokers. Persistent cookies can make it difficult to keep users’ online activity private, as they can be used to track user’s across the web. So, it is difficult to protect users’ anonymity online. 

If users are concerned about user’s privacy and online security, users may have to consider disabling or deleting persistent cookies, user can usually do this through the browser settings. However, doing so may impact users’ experience on some websites, as persistent cookies are often used to store preferences and settings. Additionally, some persistent cookies are required for certain website features to work properly, such as online shopping carts. If a user is concerned about persistent cookies but still wants to use website features that require them, the user may want to consider using a private browsing mode or incognito window. This will prevent persistent cookies from being stored on the user’s device and can help to protect the user’s privacy and online security.

Components of Persistent Cookie: There are 4 components: 

  • Cookie name: It is a unique identifier for the cookie. 
  • Cookie value: It is the data that is stored in the cookie. 
  • Expiration date: It is the date when the cookie expires. 
  • Path: It is the path on the server where the cookie is stored.

Example 1: The following code example will add a persistent cookie with a name and value which will expire in 30 days.

PHP




<?PHP
    //setcookie() defines a cookie to be 
    //sent along with the other HTTP headers
      
    // The first parameter is the name of the cookie, the second is 
    //the value, and the third is the expiry time in seconds.
    setcookie("name", "value", time() + 30 * 24 * 60 * 60); 
    // The echo command is used to print
    echo "Value of the particular name cookie will expire in 30 days"; 
    // In this case, the cookie  will expire in 30 days
?>


Output:

Value of the particular name cookie will expire in 30 days

Example 2: The following code example will remove all persistent cookies.

PHP




<?PHP
  // remove all persistent cookies
  //setcookie() defines a cookie to be
  //sent along with the other HTTP headers
  
  // The first parameter is the name of the 
  //cookie ie. username, the second is the value 
  //ie. NULL and the third is the expiry time in seconds.
  setcookie('username', '', time()-3600); 
  
  // The first parameter is the name of the cookie
  //ie. password, the second is the value ie. NULL
  //and the third is the expiry time in seconds.
  setcookie('password', '', time()-3600); 
  //Here echo command is used to print
  echo "All persistent cookies are removed"; 
  // Now, all persistent cookies are removed
?>


Output:

All persistent cookies are removed

Advantages of Persistent Cookie:

  • A persistent cookie remains on a user’s device for the period of time specified in the cookie’s max-age attribute.
  • It can be used to store information about the user, such as the user’s preferences or login status.
  • This makes persistent cookies more useful for tracking users’ Web activities over extended periods of time.
  • Persistent cookies can also be used for purposes such as storing items in a shopping cart or keeping track of a user’s progress through a form.
  • To mitigate this risk, many web browsers provide a mechanism for users to view and delete persistent cookies.
  • This can be useful for websites that offer personalized content or features, such as online stores or social media sites.
  • They can help improve the user experience on a website by storing users’ preferences and settings.

Disadvantages of Persistent Cookie:

  • They take up valuable storage space on a user’s device. They can be used to track a user’s browsing habits and collect data about them.
  • They can expire and become invalid, which can cause problems for users if they rely on them to log in to websites or access certain features.
  • They can be stolen by hackers and used to gain access to a user’s account on a website. They can be used to send spam or malware to a user’s device. They can be used to exploit security vulnerabilities in a user’s browser or device. They can be used to track a user’s location.
  • They can be used to overload a user’s device with cookies, which can slow it down.
  • They can be used to eavesdrop on a user’s browsing habits.
  • They can be used to target ads at a user based on their browsing habits.
  • They can be used to sell a user’s browsing data to third parties.
  • They can be used to bypass privacy settings in a user’s browser.
  • They can be used to launch denial of service attacks against a website.
  •  They can be used to phish a user’s login credentials.
  • They can be used to hijack a user’s session on a website.

Applications of Persistent Cookie:

  • Web servers use persistent cookies to identify individual users and understand their prior activity. This allows the server to tailor the web experience to the user by displaying content that reflects their preferences.
  • Persistent cookies can also store data such as login credentials, online shopping cart items, and preferences. This allows users to avoid having to re-enter this information every time they visit a website.
  • They are used for session management, to keep track of a user’s progress through a website.
  • They can be used for personalization, to store information about a user’s preferences and interests.
  • They can be used for security, to store information that can be used to authenticate a user, or to track a user’s activity on a website.
  • They can be used to record which websites users visit and for how long, which can be used to build up a profile of users’ interests and browsing habits.
  • They can be used to store information about a user’s social media activity and to provide targeted content and advertisements to a user.
  • They can store information about a user’s search history and provide targeted search results to a user.
  • They can store information about a user’s purchase history and provide targeted content and advertisements to a user.
  • They can also store information about a user’s location and provide targeted content and advertisements to a user.
  • They can also be used to keep items in an online shopping cart on an e-commerce website.


Similar Reads

How to serialize a cookie name-value pair into a Set-Cookie header string in JavaScript ?
In this article, you will understand to serialize a cookie with name and value into a Set-Cookie header string. Basically, here we are trying to pass the cookie with a compatible set-cookie string format. Cookie: Cookies are small blocks of data created by a web server while a user is browsing a website and are stored on the user's computer for fut
2 min read
How to parse HTTP Cookie header and return an object of all cookie name-value pairs in JavaScript ?
To parse HTTP Cookie header we will spilt the cookies data and create objects from the key-value extracted from cookies. Cookies are simply small text files that a web server sends to the user's browser. They contain the following data. Name-value pair with actual data.The expiry date for when the cookie becomes invalid.Domain and path of the serve
3 min read
Express Cookie-Parser - Signed and Unsigned Cookies
A cookie is a piece of data that is sent to the client-side with a request and is stored on the client-side itself by the Web Browser the user is currently using. With the help of cookies - It is easy for websites to remember the user's information It is easy to capture the user's browsing history It is also useful in storing the user's sessions Th
5 min read
HTTP headers | Cookie
HTTP headers are used to pass additional information with HTTP response or HTTP requests. A cookie is an HTTP request header i.e. used in the requests sent by the user to the server. It contains the cookies previously sent by the server using one or more set-cookie headers. It is an optional header. Syntax: Cookie: &lt;cookie-list&gt; In case of si
1 min read
HTTP headers | Set-Cookie
The HTTP header Set-Cookie is a response header and used to send cookies from the server to the user agent. So the user agent can send them back to the server later so the server can detect the user. Syntax: Set-Cookie: &lt;cookie-name&gt;=&lt;cookie-value&gt; | Expires=&lt;date&gt; | Max-Age=&lt;non-zero-digit&gt; | Domain=&lt;domain-value&gt; | P
2 min read
How to create and read value from cookie ?
The web servers host the website. The client-server makes a request for data from the webserver and the webserver fetches the required pages and responds to the client by sending the requested pages. The web server communicates with the client-server using HTTP (HyperText Transfer Protocol). HTTP is a stateless protocol which means the server needs
4 min read
Firebase (sign in with Google) Authentication in Node.js using Firebase UI and Cookie Sessions
Firebase Authentication provides the backend services that are easy-to-use SDKs and ready-made UI libraries to authenticate users to your app. Prerequisites: Basic knowledge of node, JavaScript and firebase. Setup: First we need to create a Firebase Project, head over to firebase Console and create a new project. I am going to name it SignInWithGoo
4 min read
How to set Cookie in ReactJS ?
Cookies are an important part of an application because cookies are used to set value in a key-value form which can be set in browser storage and used further for identifying the current user. The following example shows how to set the cookie in the ReactJS application, here we have taken the username as key, which is set in the cookie with its val
2 min read
How to create cookie with the help of JavaScript ?
A cookie is an important tool as it allows you to store the user information as a name-value pair separated by a semi-colon in a string format. If we save a cookie in our browser then we can log in directly to the browser because it saves the user information. Approach: When a user sends the request to the server then the cookie tells that this use
2 min read
Express res.cookie() Function
The res.cookie() function is used to set the cookie name to value. The value parameter may be a string or object converted to JSON. Syntax: res.cookie(name, value [, options])Parameters: The name parameter holds the name of the cookie and the value parameter is the value assigned to the cookie name. The options parameter contains various properties
2 min read
What is the purpose of the cookie-parser middleware in Express.js?
cookie-parser is middleware that simplifies handling cookies. It parses incoming cookies from client requests and makes them accessible in the req.cookies object. This makes it easier to read and manipulate cookies in your Express JS application without manual parsing. Purpose of the cookie-parser middleware in ExpressJS:Cookie Parsing: Automatical
2 min read
How to Access HTTP Cookie in Node.js ?
Cookies are small pieces of data sent by a server and stored on the client side, typically in the user's browser. They are often used to maintain stateful information such as user sessions, preferences, or tracking data. In Node.js, accessing and managing cookies is a common requirement for building web applications. This article will guide you thr
3 min read
How to Set, View and Manipulate Cookies using 'Response.cookie()' and Postman ?
Cookies enable websites to store small pieces of information on a user's device. It helps enhance user experience and enable various functionalities. In this article, we'll explore a simple way to manipulate cookies using the 'Response.cookie()' function Prerequisite:Basics of NodejsBasics of ExpressjsOne should be able to make a basic express serv
2 min read
How to manipulate cookies by using ‘Response.cookie()’ in Express?
Cookies are small data that is stored on the client's computer. Using this cookie various tasks like authentication, session management, etc can be done. In Express JS we can use the cookie-parser middleware to manage the cookies in the application. In this article, we going to manipulate cookies in Express JS by using the two scenarios/approaches
3 min read
How to create Cookie Consent Box using HTML CSS and JavaScript ?
In this article, we will see how cookies are stored using the cookie consent box using HTML, CSS, &amp; JavaScript. Cookies are small pieces of data that are stored on a user's device while browsing the website. Cookies are used to store information for faster access to data and provide a better user experience. Also, it is used for session managem
4 min read
How to Get Cookie by Name in JavaScript?
Getting a specific name in JavaScript involves parsing the document's cookie string contains all cookies in a single string separated by semicolons and space. The goal is to extract the value of a specific cookie by the name given. The Cookie is defined as a small piece of data that is stored on the user's computer by the web browser while browsing
4 min read
How to set up a cookie that never expires in JavaScript ?
We can set up a cookie that never expires in JavaScript using the following approach: Prerequisites : Intermediate level knowledge of JavaScriptBasic HTML Disclaimer: All the cookies expire as per the cookie specification. So, there is no block of code you can write in JavaScript to set up a cookie that never expires. It is just impossible and is a
3 min read
HTML DOM cookie Property
Almost every website stores cookies (small text files) on the user's computer for recognition and to keep track of his preferences. DOM cookie property sets or gets all the key/value pairs of cookies associated with the current document. Getting all the Cookies:  The document.cookie method returns a string containing semicolon-separated list of all
3 min read
How to Remove a Cookie in PHP?
Cookies are small text files stored in a user's browser. It contain data such as user preferences, session information, and other relevant details. PHP cookies are used to store information that can persist across different web pages or sessions. At times, you may need to delete or remove a cookie, for instance, to log out a user or reset their pre
4 min read
How To Fix 400 Bad Request: Request Header Or Cookie Too Large?
Have you ever seen a "400 Bad Request" error while browsing on the web, that might be confusing for you about what it means? When you request to a website the server responds with an HTTP status code. This error is among the responses in terms of the HTTP status code used by web servers in order to communicate with your browser. HTTP Status Codes t
7 min read
What are the disadvantages of using persistent connection in PDO ?
In PDO, a connection to the database can be persistent or non-persistent. Persistent connections make using the attribute PDO::ATTR_PERSISTENT. Persistent connections do not close when the script ends, but they are cached and can be reused when a requested persistent connection is identical to the existing connection. When a connection is requested
3 min read
Explain source maps in jQuery
Source maps are a powerful tool for debugging and troubleshooting code, particularly when working with large or complex codebases. They allow developers to easily understand the relationships between the original, unminified code and the compiled or minimized code that is actually running in the browser. In other words, source maps provide a way to
6 min read
Explain the concepts of functional programming in JavaScript
Every program we write follows an approach or style of writing also referred to as a paradigm. Functional programming is a declarative programming paradigm (programming paradigm where we write code in such a way that it describes the result and not the approach). To understand functional programming let's take an example of usual mathematics functi
2 min read
Explain the concept of null and its uses in TypeScript
Null refers to a value that is either empty or a value that doesn't exist. It's on purpose that there's no value here. TypeScript does not make a variable null by default. By default unassigned variables or variables which are declared without being initialized are 'undefined'. To make a variable null, we must assign Null to it. Typescript uses nul
3 min read
Explain invoking function in JavaScript
In this article, we will learn about invoking the function in Javascript, along with understanding its implementation through examples. Function Invoking is a process to execute the code inside the function when some argument is passed to invoke it. You can invoke a function multiple times by declaring the function only once. When the function is d
2 min read
Explain Action’s in Redux
In this article, we are going to learn about Action in Redux. Actions are plain JavaScript object that contains information. Action is one of the building blocks of Redux. Redux is a state managing library used in JavaScript apps. It is used to manage the data and the state of the application. Uses of Redux: With the help of redux it is easy to man
5 min read
Explain the common methods of sending request to a server in jQuery
In this article, we will see the common methods of sending requests to a server by using the jQuery library. jQuery is an open-source JavaScript library jQuery simplifies HTML document traversing and manipulation, browser event handling, DOM animation, Ajax interactions cross-browser JavaScript development. Common methods of Sending Requests to the
3 min read
Explain Asynchronous vs Deferred JavaScript
Generally, when we use a script tag to load any JavaScript code, the HTML parsing is paused by the browser when it encounters the script tag and it starts to download the JavaScript file first. The HTML elements script tag will not be executed until the browser is done with downloading the script and executing it. The browser waits till the script
3 min read
Explain Dialogs in Materialize CSS
Dialogs in Materialize CSS provide users additional information to users if needed by them. These are directly not visible on the web-page. The dialog transitions are related to the information that is required at that point of time. Materialize provides various methods to show dialogs. Syntax: Materialize.toast(string, time, styleClass,callback);
2 min read
Explain Chosen and Select2 with examples
The two jQuery plugins, Chosen and Select2 are used to style the select boxes. It improves the look of select boxes, enhances its behavior, thus making them much more user-friendly. They can be used for both single select boxes and multiple select boxes. These jQuery libraries need to be added: select2.min.js select2.min.css chosen.jquery.min.js ch
4 min read