Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookie expiration date to a month?

i just need to set this function to expire in one month, can someone tell me how to do it?

function _saveUserPreference() {
    // Set the cookie expiry to one year after today.
    var expiryDate = new Date();
    expiryDate.setFullYear(expiryDate.getFullYear() + 1);
    document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString();
}
like image 202
Ilie Avatar asked Nov 13 '14 02:11

Ilie


People also ask

How do you set cookies to expire in 30 days?

time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes). The path on the server in which the cookie will be available on.

How do you set the expiry period for a cookie object?

Just set the expires parameter to a past date: document. cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; You should define the cookie path to ensure that you delete the right cookie.

How do you set a cookie for one year?

php setcookie("TestName", "Test Value", time()+3600 * 24 * 365); ?> >> Here 'TestName' is name of cookie. >> "Test Value" is value to store. >> time()+3600 * 24 * 365 - will set cookie time till 1 year.

How do you handle cookie expiration?

Cookies with an expiration date in the past will be removed from the browser. To remove a cookie, you must set it's set its expiration date in the past. This will signal to the browser that the cookie should be removed. For cleanliness, it's also a good idea to set its value to an empty string.


1 Answers

You need add one month to the expiryDate var:

  var expiryDate = new Date();
  expiryDate.setMonth(expiryDate.getMonth() + 1);
like image 122
ianaya89 Avatar answered Sep 30 '22 09:09

ianaya89