Open In App

Explain Persistent Cookie

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:

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: 

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






<?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
  // 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:

Disadvantages of Persistent Cookie:

Applications of Persistent Cookie:


Article Tags :