PHP 会话发送标头“Cache-Control:no-store、no-cache、must-revalidate”。如何更改为“Cache-Control: s-maxage=10, public, max-age=10”

发布于 2025-01-14 11:05:20 字数 458 浏览 4 评论 0原文

PHP 会话导致每个页面都包含标头 cache-control: no-store, no-cache, Must-revalidate

我需要重写此行为并将其更改为 Cache-Control: s-maxage=10, public, max-age=10 甚至只是 cache-control: public, max-age= 10..

我尝试使用会话变量 session_cache_limiter('public');session_cache_expire(1); 但是,过期值以分钟为单位,我无法弄清楚如何将其设置为 10 秒。

如何将 session_cache_expire(1); 设置为 10 秒? 如果这是不可能的,我还能如何覆盖会话标头并将缓存控制设置为 10 秒?

PHP session is causing each page to contain the header cache-control: no-store, no-cache, must-revalidate.

I need to override this behavior and change it to Cache-Control: s-maxage=10, public, max-age=10 or even just cache-control: public, max-age=10.

I've tried to use session variables session_cache_limiter('public'); and session_cache_expire(1); but, the expire value is in minutes and I cant work out how to set it to 10 seconds.

How can I set session_cache_expire(1); to 10 seconds?
If that's not possible, how else can I override the session header and set the cache control to 10 seconds?

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

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

发布评论

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

评论(3

謸气贵蔟 2025-01-21 11:05:20

在@Amikot40 和@CBroe 的帮助下,我解决了这个问题。

// remove pragma no-cache header with session variable
// this also sets the Cache-Control header, but you will change that after session start
session_cache_limiter('public');

session_start();

// cache for 10 seconds
header("Cache-Control: s-maxage=10, public, max-age=10");

// expire in 10 seconds
$expire_time = new DateTime('UTC');
$expire_time->add(new DateInterval('PT10S')); // add 10 seconds
$expire_time = $expire_time->format(DateTimeInterface::RFC7231);
header("Expires: $expire_time");

With the help of @Amikot40 and @CBroe, I've solved the issue.

// remove pragma no-cache header with session variable
// this also sets the Cache-Control header, but you will change that after session start
session_cache_limiter('public');

session_start();

// cache for 10 seconds
header("Cache-Control: s-maxage=10, public, max-age=10");

// expire in 10 seconds
$expire_time = new DateTime('UTC');
$expire_time->add(new DateInterval('PT10S')); // add 10 seconds
$expire_time = $expire_time->format(DateTimeInterface::RFC7231);
header("Expires: $expire_time");

国际总奸 2025-01-21 11:05:20

在 PHP 中,您使用 header() 函数:

<?php
  header("Cache-control: public, max-age=10");
  header("Expires: Sat, 1 Apr 2022 05:00:00 GMT");
?>

替代选项是在顶部使用 HTML 标签 - 在第一个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Cache-control" content="max-age=10" />
    <meta http-equiv="Expires" content="Sat, 1 Apr 2022 05:00:00 GMT" />
</head>
<?php

// your php code starts here. 

如果您使用任何具有模板的前端框架 - 您应该在那里查找元标记,或者可能在其中查找元标记框架文档(可能有专门的功能)。

In PHP you use header() function:

<?php
  header("Cache-control: public, max-age=10");
  header("Expires: Sat, 1 Apr 2022 05:00:00 GMT");
?>

Alternate option is to use HTML tags on top - before first <?php opening:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Cache-control" content="max-age=10" />
    <meta http-equiv="Expires" content="Sat, 1 Apr 2022 05:00:00 GMT" />
</head>
<?php

// your php code starts here. 

If you using any frontend framework which has its templates - you should look for meta tags there, or possibly in that framework documentation (possibly there are dedicated functions for that).

放低过去 2025-01-21 11:05:20
// remove pragma no-cache header with session variable
// this also sets the Cache-Control header, but you will change that after session start
session_cache_limiter('public');

session_start();
// cache for 10 seconds
header("Cache-Control: s-maxage=10, public, max-age=10");

// expire in 10 seconds
$expire_time = new DateTime('UTC');

$expire_time->add(new DateInterval('PT10S')); // add 10 seconds
$expire_time = $expire_time->format(DateTimeInterface::RFC7231);
header("Expires: $expire_time");
// remove pragma no-cache header with session variable
// this also sets the Cache-Control header, but you will change that after session start
session_cache_limiter('public');

session_start();
// cache for 10 seconds
header("Cache-Control: s-maxage=10, public, max-age=10");

// expire in 10 seconds
$expire_time = new DateTime('UTC');

$expire_time->add(new DateInterval('PT10S')); // add 10 seconds
$expire_time = $expire_time->format(DateTimeInterface::RFC7231);
header("Expires: $expire_time");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文