We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26439
    • 104 Posts
    I have a piece of code that sniffs out if there is a cookie set or a variable passed in the url and if so puts it into a modx placeholder. It works great, I can then use modx->getPlacehouder to access that variable in the code, pass it to the stylesheet and access it from snippets....

    <?php
    if(isset($_GET[user_style])) // if user_style has been requested by the user in the url
    {
          $user_style = $_GET[user_style]; // define user_style
          setcookie("user_style",$_GET[user_style],time() + (86400 * 30), "/", "", 0); // set cookie for 30 days
    }else  // else if there is no user_style variable in the url
    {
       if(isset($_COOKIE['user_style'])) // see if there is a cookie
          {
          	$user_style = $_COOKIE['user_style']; // if there is a cookie set user_style to cookie user_style
          }else{
          	$user_style = 0; // if not use default
          }
    };
    $modx->setPlaceholder(user_style, $user_style);
    ?>
    


    ... however I’m having major trouble trying to get that code working in as a "snippet", I’ve tried various methods, setting a temp placehouder to avoid having the "_GET" query in the url.

    Question, is it worth spending any more time on, as I am only doing it to keep things "tidy"?

    Secondly, is there some syntax or a method to do this that escapes me?



      • 26439
      • 104 Posts
      Simple solution, put it into an include file.....

      <?php
      include ("assets/snippets/cookie_sniffer.php");
      ?>
      
        • 30223
        • 1,010 Posts
        I just tried the example in 0.9.5 after cleaning up the code a little and I had no trouble getting it to work, with the place holder in the document or in the template. If you are having problems accessing the value from another snippet it may be because the ’first’ snippet has not been evaluated by the time you’re running the "second" snippet.

        Here’s the cleaned up code, basically only added missing quotes around user_style and added a return statement at the end of the snippet. Perhaps technically not needed but it’s good practice and avoids any php notice errors (if they are turned on at any time).
        <?php
        if(isset($_GET['user_style'])) // if user_style has been requested by the user in the url
        {
              $user_style = $_GET['user_style']; // define user_style
              setcookie("user_style",$_GET['user_style'],time() + (86400 * 30), "/", "", 0); // set cookie for 30 days
        }else  // else if there is no user_style variable in the url
        {
           if(isset($_COOKIE['user_style'])) // see if there is a cookie
              {
              	$user_style = $_COOKIE['user_style']; // if there is a cookie set user_style to cookie user_style
              }else{
              	$user_style = 0; // if not use default
              }
        };
        $modx->setPlaceholder('user_style', $user_style);
        return '';
        ?>