在执行的任务中,我需要创建一个在用户会话中保留的布尔变量,因此想到了一个cookie(我使用的是Prestashop,因此应该是$this->context->cookie->myVariable
)。
用户将选中页面中的复选框,这将触发对脚本的AJAX请求,该脚本会更改相关Cookie的值。在AJAX请求成功之后,我期望基于cookie的值,进一步的请求/重新加载到网页将呈现稍有不同。
我的方法在localhost上可以很好地工作,但是在我们的生产网站(包括Cloudflare CDN)上,它一直显示cookie变量的旧值。这可能是CDN缓存旧的cookie值吗?还是我在这里做错了什么?
下面是设置变量的PHP脚本:
<?php
include_once('./config/config.inc.php');
include_once('./config/settings.inc.php');
include_once('./classes/Cookie.php');
$context = Context::getcontext();
//switching the value of myVariable
if(!$context->cookie->myVariable) {
$context->cookie->__set('myVariable',true);
}
else {
$context->cookie->__set('myVariable',false);
}
die("Success!");
以及ajax请求的Javascript代码:
$.post("url/of/script",null,function(res) {
if(res.includes("Success")) {
//do something on success
//I expect this request to have changed the cookie's value,so that on further requests I would get the right results displayed. It is not the case on production site only.
}
});