<html> <body> <?php var_dump ($_COOKIE); ?> <script> document.write (document.cookie); document.cookie = 'abc=def'; </script> </body> </html> this piece of code doesnt retain the cookie between browser shutdown and browser restart. i'm really puzzled as to why. can someone please explain this to me?
Try adding more params see if it sticks: document.cookie ="abc=def, expires=Fri, 31 Dec 9999 23:59:59 GMT, path=/, domain=example.com"; Code (markup):
but setting it with an expires, a path, and *no* domain, that does work.. strange, this used to work out of the box thanks for the extra eyeballs folks
Omitting the expiry often just flat out doesn't work... legacy IE will in fact automatically treat it as a zero expiry so it wouldn't even be retained across normal refreshes, much less a browser restart. ALWAYS include an expiration time. Also, it's SUPPOSED to be semi-colons, not commas! That too could be screwing it up. document.cookie ="abc=def; expires=Fri, 6 Jun 3052 15:00:00 GMT; path=/; domain=example.com"; That SHOULD do the trick.... assuming the domain actually matches. GENERALLY I don't bother stating the domain though since the default is the domain the site is on. You only set that when trying to restrict it to specific subdomains. I have a mantra -- When in doubt, check MDN: https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie see, SEMI-COLONS, not commas!
Rfc2965 says it is preferred to be a semicolon but should also work with a comma. However, I noticed that the comma specification portion was removed on the updated rfc6265, so I guess it is better to stick with the semicolon. I do have a feeling that many browsers would still enable both for compatibility.