We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    I am trying to set a cookie on one page with one value, then change that value on another page, then depending on the value display a chunk. However I am having a problem in refreshing the value of the cookie. I have never used cookies before so I may be doing something wrong.

    Anyway, I figured that I could set a cookie by adding this snippet to the top of one of my templates which in this case means it is processed on every page:
    <?php
    $cName = isset($cName) ? $cName : 'myCookie';
    $cValue = isset($cValue) ? $cValue : '';
    $cTime = time()+604800;
    setcookie($cName,$cValue,$cTime);
    ?> 


    And I get the chunck with this snippet:
    <?php
    $cName = isset($cName) ? $cName : 'myCookie';
    $cValue = isset($cValue) ? $cValue : '';
    $ifChunk = isset($ifChunk) ? $ifChunk : '';
    $elseChunk = isset($elseChunk) ? $elseChunk : '';
    $defChunk = isset($defChunk) ? $defChunk : '';
    
    if(isset($_COOKIE[$cName])) {
    	if($_COOKIE[$cName] == $cValue) {
    		return $modx->getChunk($ifChunk);
    	} else {
    		return $modx->getChunk($elseChunk);
    	}
    } else {
    	if($defChunk == 1){
    		return $modx->getChunk($ifChunk);
    	} else {
    		return $modx->getChunk($elseChunk);
    	}
    }
    
    ?>

    The setup works, however I am unable to update the value of the cookie. Once I set the cookie it only changes once I edit the document in question otherwise it stays the same. I called the snippet uncahed and turned of the page cache but still no go. If on one page I change the value it will not change until the page is edited.

    I am using a tv to determine the value of the cookie in the snippet call:
    [!CookieBaker? &cName=`myCookie` &cValue=`[*tvValue*]`!]


    I am 100% sure that this is a problem of mine. Anyone have an ideas what could be going wrong?
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don&#39;t know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 33372
      • 1,611 Posts
      Is the tvValue TV dynamic in some way (e.g., calculated using an @EVAL)? I ask because otherwise I don’t see how the cookie value is going to change using that snippet. The expiration time should change, but the value looks as if it would be the same every page load...
        "Things are not what they appear to be; nor are they otherwise." - Buddha

        "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I would suppose that the TV value is set differently for each page. Would not the SESSION actually be easier for this?
          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
          • 7231
          • 4,205 Posts
          The tv is a text TV that has a unique value in each document. For docs without a cookie it will load the default chunk.

          SESSION you say...hummmm, did not think of that. I am a php novice so I will need to pull out the old manual to remember how to use SESSION. So instead of setting a cookie it would set a session var, sound straight forward. And the advantage here would be that it would not be dependent on cookies.

          Just to make sense of things, is there any reason why a cookie would not work for this as well? The advantage being that the value could be recalled at a later date rather than starting fresh every session. I have the day off so I may poke around with this a bit more, but I need to deliver the site soon so I may just switch to session vars and move on

          Thanks..
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 30223
            • 1,010 Posts
            And the advantage here would be that it would not be dependent on cookies.

            This is not entirely correct as the sessions propagate using cookies as well.

            As far as I can see there’s nothing wrong in principal with your code, however when you set a cookie it is not visible to php until the next page load (or the same page is reloaded). So if for instance you set a cookie value of 100 in page A and a value of 200 in page B and a visitor moves from page A to page B on page B to page C you (or php) will see a value of 100 in page B and a value of 200 in page C.

            If you are loading a chunk based on a cookie the chunk you are displaying is not dependent on the (cookie) value of the current page but on the cookie value of the previously visited page. I’m not quite sure what you want to achieve with setting a cookie this way, unless you want exactly the behavior I just described. As you are resetting the cookie at each page that loads to a new value you are not really saving anything in the viewer’s browser other than the value belonging to the last page they visited.
              • 33372
              • 1,611 Posts
              A SESSION var would be available immediately, however, which would be an advantage over using only cookies (both do write a cookie file, as Toby points out). Are you sure that the cookie value is not being modified when you load another page, or are you just assuming so because you’re not seeing the chunk content change the way that you expect? Toby’s other point that cookies set on a page load are not available until the following page load might explain such unexpected results.

              I find that tools like this one can be very useful for debugging cookie code: https://addons.mozilla.org/en-US/firefox/addon/315
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                • 7231
                • 4,205 Posts
                however when you set a cookie it is not visible to php until the next page load
                That describes the exact problem I was having. As you say it seemed not to be working. At least it was not a bug.

                If you are loading a chunk based on a cookie the chunk you are displaying is not dependent on the (cookie) value of the current page but on the cookie value of the previously visited page. I’m not quite sure what you want to achieve with setting a cookie this way, unless you want exactly the behavior I just described.
                The scenario is that I have a two language site, however there are a number of documents that are actually shared by both languages (they can’t be duplicated and need to be easily updatable by the client). With the cookie setup I could load the appropriate header and section navigation according to the cookie value. However, if the new cookie value will only be available on the next page, this creates a problem since I will not get the value on the current page. Since the SESSION is instant that might be the better way to go for this.

                Thanks for the help.
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]