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?