如何在 ESI:include 脚本中设置 cookie?

发布于 2024-10-25 00:53:19 字数 654 浏览 0 评论 0原文

我有一个通过 Varnish 加载的基本 PHP 页面,其中包含一个 ESI 包含回调到服务器以设置 cookie。 cookie 是通过域访问等设置的,但是当通过 ESI 调用时,永远不会设置 cookie。如果直接访问 ESI 包含路径,则设置 cookie 不会出现问题。我什至将 Varnish 配置设置为从不缓存任何内容,认为 VCL 可能会杀死 cookie。

这...

<esi:include src="/init.php?<?=http_build_query($_GET); ?>"></esi:include>

...包括这个...

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

setcookie('superman', 'clark kent', 0, '/', '.whatever.com');

?>

我是 Varnish 和 ESI 的新手,所以我开始怀疑这是否是一个已知的限制(无论是使用 ESI 还是使用 Varnish 的 ESI 实现),但我不能在线查找有关我的问题的任何讨论。

I have a basic PHP page being loaded through Varnish with a single ESI include that calls back to the server to set a cookie. The cookie is set with domain access and the like, but when called through ESI the cookie is never set. If you access the ESI include path directly, the cookie is set with no issue. I have even set my Varnish configuration to never cache anything, thinking the VCL could be killing the cookie.

This...

<esi:include src="/init.php?<?=http_build_query($_GET); ?>"></esi:include>

...includes this...

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

setcookie('superman', 'clark kent', 0, '/', '.whatever.com');

?>

I'm new with Varnish and ESI, so I'm starting to wonder if this is a known limitation (either with ESI or with Varnish's ESI implementation), but I can't find any discussion of my problem online.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2024-11-01 00:53:19

一个有趣的问题,之前已经被问过,但没有得到解答(通过 ESI:include 设置 Cookie,如何?)。我认为你不能这样做。通过 ESI-include,ESI 处理器发出单独的请求并替换正文中的一部分,而不是标头。为了使您首选的 set-cookie 行为正常工作,ESI 规范应指定如何“合并”所有 set-cookie 标头。

请参阅 ESI 规范第六章:http://www.w3.org/TR/esi-语言

处理 ESI 模板时,
需要提出单独的请求
对于遇到的每个包含。
实现可以使用原来的
请求的标头(例如 Cookie、
用户代理等)这样做时。
此外,响应头来自
片段(例如,Set-Cookie、服务器、
缓存控制,最后修改)可能是
被忽略,并且不应该影响
组装页面。

您可以尝试在 javascript set-cookie 脚本中转换您的 set-cookie 标头吗?这可以包含在正文中......

An interesting question which has been asked -but not answered- before ( Setting Cookies via ESI:include, how? ). I don't think you can do it this way. With ESI-include, the ESI processor makes a separate request and replaces a part from the body, not the header. In order to make your preferred set-cookie behavior work correctly, the ESI specification should specify how to 'merge' all set-cookie headers.

See chapter six of the ESI spec: http://www.w3.org/TR/esi-lang

When an ESI template is processed, a
separate request will need to be made
for each include encountered.
Implementations may use the original
request's headers (e.g., Cookie,
User-Agent, etc.) when doing so.
Additionally, response headers from
fragments (e.g., Set-Cookie, Server,
Cache-Control, Last-Modified) may be
ignored, and should not influence the
assembled page.

Could you try to convert your set-cookie header in a javascript set-cookie script? This could be included in the body...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文