1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

cookies not remembered between browser shutdown and browser restart? :(

Discussion in 'Programming' started by seductiveapps.com, Mar 27, 2018.

  1. #1
    <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?
     
    seductiveapps.com, Mar 27, 2018 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    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):
     
    ThePHPMaster, Mar 27, 2018 IP
  3. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #3
    it doesn't :(

    not even when i set it to Tue, 31 Dec 2019 instead
     
    seductiveapps.com, Mar 27, 2018 IP
  4. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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
     
    seductiveapps.com, Mar 27, 2018 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    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!
     
    deathshadow, Mar 29, 2018 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    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.
     
    ThePHPMaster, Mar 29, 2018 IP