We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6705
    • 79 Posts
    Can anybody offer any advice on best coding practice on setting cookies in snippets?

    This is the first time that I’ve tried to write a page that uses cookies, so I’m on a double learning curve here. Setting cookies of course needs to be done as the HTTP headers are transmitted, before any page content is output. Nevertheless, I knocked together a test page which includes a form (which submits to the same URL), and included code in the snippet (if ($submitted), etc..) which sets a cookie based upon the form input.

    The page is partly-working (the cookie seems to be set, but I’m not always getting expected behaviour), although this is probably as much due to the fact that I haven’t yet debugged my test code as anything else, but I was wondering if there was anything about setting cookies in MODx that I need to be aware of.

    My snippet doesn’t display any output until after the cookie is set, but obviously the snippet is embedded within a page (which does have other content), and the page itself is wrapped in a template. Does MODx go through and parse all of this first before generating the output? (That is, even though the cookie-setting code is "ultimately" contained within other content, will MODx execute the code and set the cookie properly before it outputs all of the other containing content that it parses?)

    Thanks for any advice.
      Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
      • 6705
      • 79 Posts
      Sorry, just bumping this thread..

      Does anybody have any good coding practice guidance on setting cookies in MODx snippets?
        Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
        • 26931
        • 2,314 Posts
        Hi david55,
        unfortunatelly i can’t answer your questions, but there’s a snippet called "Cookie Monster". maybe it will be helpful for you (or parts of the code): http://modxcms.com/extras/package/618