0

I've researched and I've found how to delete a cookie by name, however, I am unsure how/or if its possible to delete a cookie which is older than a specific time period i.e. delete cookie if its 24hours old. So far I have this:

var delete_cookie = function(name) {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};


delete_cookie('roundcube_sessauth');

I would assume (again if possible) I would need to set variable to actual time and if the cookie time is 24h older than real time, delete?

Thanks in advance.

ben_dchost
  • 875
  • 4
  • 13
  • 24
  • Do old cookies (with expiration date) even exist? – A1rPun Jun 17 '14 at 09:38
  • @A1rPun I don't know. But basically my website has a small problem with cookie remembering a 'basket' of a customer, which than affects how the website behaves. Ideally, I want the website to check the users browser for a cookie, if the cookie is older than a day, then remove the cookie so the website will treat the customer as a first time user again – ben_dchost Jun 17 '14 at 09:44
  • What about actually setting the proper expiration time? The browser should clear expired cookies automatically. – Cerbrus Jun 17 '14 at 09:46
  • @Cerbrus that's probably the best way to do it... however, I am unsure where the cookie is 'created' since most of the functionality of the website is created through an API. Could I create a new function and just set the cookie expiration date (I think it's 'created' on basket page, but no sign of it)? – ben_dchost Jun 17 '14 at 09:54
  • You could just "get" the cookie, and set an expiration date, yea. It's not an elegant solution, but it should work. – Cerbrus Jun 17 '14 at 09:58
  • @Cerbrus Thanks, so would I need to implement something like [this](http://stackoverflow.com/questions/3794989/how-to-set-a-cookie-to-expire-in-1-hour-in-javascript)? Also, would that mean every time the user goes on the page, if the cookie hasn't expired would the time be reset? – ben_dchost Jun 17 '14 at 10:01
  • Something like that, yes. Unless you check for the expiration date, which is a bit of a hassle. And that's exactly why it should really be done when the cookie is first set. – Cerbrus Jun 17 '14 at 10:05
  • @Cerbrus thanks for your help, if you want you can post an answer with your method so I can reward you for your help! :) – ben_dchost Jun 17 '14 at 10:07

1 Answers1

0

You'll probably want to set the expiration time of those cookies, properly.

Preferably, this is done when you first set the cookies. If that code can't be accessed, you could work around it by getting the cookie's contents, and setting a new cookie with a proper expiration time.
(Just make sure you check for incorrect expiration times, first)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • I've just looked at the cookie and it expires "When the browsing session ends". Do you know if it's possible to add an additional condition so it 'deletes' the cookie if browser session ends OR cookie been created for longer than (an hour or so). – ben_dchost Jun 17 '14 at 10:35
  • Cookies either expire when the session ends, _or_ at a specific time. What you described isn't possible with how cookies work. – Cerbrus Jun 17 '14 at 11:15
  • Ok I will just have to deal with it then. Thanks again! – ben_dchost Jun 18 '14 at 07:36