Chrome does not allow cookies after less than 4 hours

I cannot set short-life cookies in Google Chrome. They are either not installed or are immediately deleted (I can’t say what the result is, although the result is the same anyway). This only happens if the expiration date is 4 hours or less. The identical code works fine if the expiration time exceeds 4 hours and the problem does not occur in Firefox or Safari. Here is an example:

Does not work:

exp = new Date(); exp.setMinutes(exp.getMinutes() + 240); document.cookie="name=value;expires=" + exp + ";path=/"; 

Works:

 exp = new Date(); exp.setMinutes(exp.getMinutes() + 241); document.cookie="name=value;expires=" + exp + ";path=/"; 

Does anyone have any suggestions to fix this?

+4
source share
2 answers

Indeed, I checked the Chromium source here http://code.google.com/p/chromium/source/search?q=document.cookie+expire&origq=document.cookie+expire&btnG=Search+Trunk with a link to cookies and found in of all their expires = operators, which they call either .toGMTString () or .toUTCString () in the date object, so it can be a kind of date formatting function that is screwed when it explicitly converts it to a format rather than explicitly defining it.? !

instead of this:

 document.cookie="name=value;expires=" + exp + ";path=/"; 

try the following:

 document.cookie="name=value;expires=" + exp.toUTCString() + ";path=/"; 
+3
source

Seems to work for me using jQuery.cookie:

 Command: exp = new Date() Output: Thu Aug 09 2012 11:39:21 GMT-0700 (Pacific Daylight Time) Command: exp.setMinutes(exp.getMinutes() + 240) Output: 1344551961739 Command: $.cookie('testCookie', 'test', {path: '/', expires: exp}); Output: "testCookie=test; expires=Thu, 09 Aug 2012 22:39:21 GMT; path=/" 

This was done in the Chrome console on the windows.

Note: 22:39 GMT - 15:39 GMT -0700, so this is the expiration of 4 hours.

Edit: I checked your code directly and it does not seem to agree that the cookie expires in less than 4 hours. This does not use jQuery:

 exp = new Date(); exp.setMinutes(exp.getMinutes() + 240); document.cookie="testCookie2=test;expires=" + exp.toUTCString() + ";path=/"; 
+1
source

All Articles