We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6705
    • 79 Posts
    Cookies, as we all know, need to be set in the HTTP headers, before a page sends any output.

    I'm now in the position where I need to be able to set a cookie when a visitor saves a preference. The first thing that springs to mind would be to have a snippet in the page, with a simple form, which calls the same page when it is submitted (and sets the cookie accordingly, if so). Of course, even if I make the snippet the first thing on the page, output will already have started to be sent from the page template up until that point is reached. sad

    I know that the way in which MODx parses pages can perform certain magic to allow you do some things with hindsight (such as "retrospectively" adding a script file to the 'head' section using regClientStartupScript, while the page is assembled, and it all sorts itself out in the end.

    Is there similar magic available to do the same for setting cookies, or is my only option to have the form submit to a special page (with no template or output) which will set the cookie headers, and then do some 'referer' divination to send the visitor back where they came from, along with their freshly-baked cookies?
      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
      ..Some further manual-reading later..

      It looks as though ob_start and its friends might help me out here.

      Now to work out which combination of server variables concocted together will give me "this page itself" for use in the "Location" header..
        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.
        • 31946
        • 116 Posts
        Edit:
        Sorry I just realized that this was posted in an evo topic. My solution is for revo... i don't know evo enough, maybe there's something similar?

        You could set cookies with a plugin listening on the "OnHandleRequest" event.
        In the plugin, check for the resource-id that contains the form/snippet.
        //Sample Code...
        if($modx->resource->get('id') == 10){
          //Set cookie here...
        }
        [ed. note: Wanze last edited this post 11 years, 11 months ago.]
          • 4041
          • 788 Posts
          Create a new snippet with the code below, named whatever (for this example it's named set_cookie). Add the snippet call to a document or template:
          [!set_cookie!]

          <?php
          $output ='';
          
          if (isset($_POST['set_cookie'])) {
              // set the cookie
              setcookie("TestCookie", $_POST['set_cookie']);
              // refresh the page
              $modx->sendRedirect($modx->makeURL($modx->documentObject['id']));
          }
          $cookie = print_r($_COOKIE,true);
          $output .='<pre>'.$cookie.'</pre>';
          
          
          $output .='
          <form name="cookie_setter" method="post" action="">
          <input name="set_cookie" type="text" maxlength="255" value="">
          <input type="submit" value="go">
          </form>';
          
          return $output;
          ?>
            xforum
            http://frsbuilders.net (under construction) forum for evolution