Setcookie syntax Google Chrome
Asked Answered
G

2

20

I am using following syntax to set cookie:

Set-Cookie:Cookie-name=value; path=/; Max-Age=1296000; HttpOnly

In google chrome console it is showing Invalid Date for that cookie.

What is wrong in the syntax ?

According to http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age http://tools.ietf.org/html/rfc6265#section-5.2.2 I can use Max-Age to specify relative expiration time.

Glasshouse answered 13/2, 2012 at 5:43 Comment(9)
As per the Wikipedia page, if you'd followed the link to the RFC in question, it clearly states that not all useragents support max-age and will ignore it if found.Agrology
@MarcB It is setting max-age as correct value in Google-chrome. I don't know then why it is showing "Invalid Date"Glasshouse
I am seeing the same issue in Chrome 17.0.963.56. The other issue I am seeing is that it doesn't show subsequent cookies in the same Set-Cookie header.Surety
yep, seeing the same here. simple php setcookie('x','y',123+time(),'/','.domain.com'); shows up as invalid cookie.Jett
What version of Chrome are you running? I could not replicate the error.Islek
@Islek Version 24.0.1312.57. You need to record. Page where you are setting cookie will show that error. Next page will not show that error in webview.Glasshouse
@VivekGoel, btw, according to RFC 6265 tools.ietf.org/html/rfc6265#section-4.1.1 you need a space after Set-Cookie:. Not that any browsers would complain of course...Columnist
@Columnist ok. thanks. But this is not problem here. I tried by giving space also. It is reporting same error.Glasshouse
@VivekGoel, I know it's orthogonal to the question.Columnist
O
6

I don't think it is something which you could/should solve. The Inspector/Dev tool that you are using is not always right as shown by the other user. There are other cases where the Dev tool is wrong too, like in "network" tool.
You could install an extension like edit this cookie to find out how your cookies are behaving. Although it doesn't help you track across redirects, it helps knowing what cookies are set up and allows you to change it too.

Overjoy answered 12/2, 2013 at 8:1 Comment(0)
I
4

Update

I performed additional tests and came to the same conclusion as Issue 123013 listed below. I think this is just a DevTools bug.

DevTools > Network appears to incorrectly render the date. However, DevTools > Resources correctly displays the date similar to other browsers.

Also, another cookie I created correctly expired. This was reflected in DevTools > Resources but not in DevTools > Network. .

Test Results

setcookie('foo', 'bar', time()+3600, "/", NULL, false, true);

Chrome v24.0.1312.57
  Resources > Cookies   Thu, 14 Feb 2013 17:08:33 GMT
  Network > Cookies     Invalid Date

Firefox 18.0.2          Thursday, February 14, 2013 11:59:15 AM
IE9 9.0.8112.16421      Thu, 14-Feb-2013 17:06:42 GMT

Finally, to conclude, your syntax appears correct. The message "Invalid Date" looks like a Chrome bug.


I could not replicate this issue with Chrome v24.0.1312.57. I set various test cookies with JavaScript and PHP. Chrome threw no errors and correctly converted the max-age values I passed into a corresponding expires values.

This bug, or something very similar, has been submitted and apparently fixed.

WebKit

Chromium

Islek answered 8/2, 2013 at 2:55 Comment(1)
It is not fixed. Try this in php - setcookie('blah', 'blah', time()+100, "/", NULL, false, true); BTW, I tested on ubuntu 12.04 with same version of chrome you reported in your answer.Overjoy

© 2022 - 2024 — McMap. All rights reserved.