Prestashop cookie被缓存到旧值-可能是CDN问题吗?

在执行的任务中,我需要创建一个在用户会话中保留的布尔变量,因此想到了一个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.
    }
 });
wowmm 回答:Prestashop cookie被缓存到旧值-可能是CDN问题吗?

经过进一步研究,我发现this answer挽救了我的生命!

我确实知道Cache-control标头,并在发布此问题之前尝试使用它,但是它不起作用。原来,我需要将其放置在 之前,而不是之后! Location标头也是如此,由于要实现的功能,我目前还添加了它。

底线:如果要使用Cache-controlLocation标头,请在更改Cookie之前 而不是之后指定它们!

我更新的PHP代码:

<?php

include_once('./config/config.inc.php');
include_once('./config/settings.inc.php');
include_once('./classes/Cookie.php');

//the mighty headers!!
header("Location: $redirect_url"); //redirect - a functionality I need now
header('Cache-control: no-store,no-cache,must-revalidate'); //cache control header


$context = Context::getContext();

if(!$context->cookie->made_in_lb) {
    $context->cookie->__set('myVariable',true);
}

else {
    $context->cookie->__set('myVariable',false);
}
本文链接:https://www.f2er.com/3063150.html

大家都在问