We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    Hello, I installed cookieJar, and it is working correctly setting a custom cookie, but I have to do a manual refresh on my page in order to get the correct value for getCookie. Here is what I am doing:

    I am working on displaying different content on my page depending on what location a user is coming from, so in my head I have:
     [[!setCookie? &name=`cookie_market` &value=`[[*cookie_market]]` &expires=`2592000` &path=`/`]]

    ([[*cookie_market]] is a value set by a geoIP service)

    I have a dropdown menu chunk to select a location:
    <div class="select-location">
        <div class="selected"><a href="[[*cookie_market]]/">[[!menuPlaceholder]]</a></div>
        <div class="pulldown">
            [[Wayfinder? &startId=`0` &level=`1` &limit=`6`]]
        </div>
    </div>


    I have a snippet called menuPlaceholder that sets the value for the selected item in my dropdown options:
    <?php
    switch (($_COOKIE['cookie_market'])) {
        case "new-orleans":
            return 'New Orleans';
            break;
        case "st-louis":
            return 'St. Louis';
            break;
    }


    I also have getCookie set up on my logo, which is also not refreshing when I switch between markets:
    <a href="[[!getCookie? &name=`cookie_market`]]" class="logo"><img src="assets/images/logo.svg"></a>


    The issue happens when I switch between markets, inspecting the page I can see that my cookie is being set correctly for the new market, however, my placeholder text is either blank or displays the old value and the logo links to the old location. When I manually refresh the page, then the logo link and display name show correctly.

    I added the ! to not cache my snippet and get/setCookie, but that doesn't seem to be working, can someone please help me with any suggestions on how to update my page values without having to do a manual refresh. Thank you very much.
    • Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
      http://php.net/manual/en/function.setcookie.php

      One of the comments has a suggestion for "live cookies"
      <?php
          function SetCookieLive($name, $value='', $expire = 0, $path = '', $domain='', $secure=false, $httponly=false)
          {
              $_COOKIE[$name] = $value;
              return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
          }
      
          function RemoveCookieLive($name)
          {
              unset($_COOKIE[$name]);
              return setcookie($name, NULL, -1);
          }
      ?>

      He sets (or unsets) the $_COOKIE value so that your $_COOKIE-reading code has something to work with. Of course, this code would have to be run before your code that uses the $_COOKIE value.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • The user requests a page. When MODX runs your snippets, the location is determined, and a cookie is defined based on that location. You don't have a cookie from the user yet. The page is generated and sent to the user, along with the new cookie. Now the cookie is set in his browser. It will get sent to the server along with the next request for another page - hence the need to reload the page before the cookie takes effect.

        What you need to do is get the location and use that value while generating the first page. You could have PHP code use it, but that would duplicate your cookie-checking code. The example I used above manually sets the $_COOKIE value so your cookie-checking snippet can use it just as if it had been sent along with the page request. The manual $_COOKIE value must be set before it's used, of course.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 48586
          • 78 Posts
          Thank you, that makes perfect sense, for some reason that didn't click earlier. I really appreciate it.
          • Sometimes the logic of what's going on between the server and the browser is hard to follow.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org