如何在 php/ 中编写未来 8 小时的 HTTP 响应标头

发布于 2024-09-25 05:54:08 字数 203 浏览 0 评论 0原文

我知道如何在 PHP 中设置基本的过期 HTTP 响应标头,如下所示...

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

但是,我想让它更加动态一点,因为我想使用 PHP,在用户访问时提前八小时指定过期时间内容。有人可以帮助我实现这个目标吗?

提前致谢!

I know how to set a basic expires HTTP response header in PHP as follows...

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

However, I want to make it a little more dynamic in that I want to, using PHP, specify an expiration time eight hours ahead of when the user accesses the content. Could someone help me achieve this?

Thanks in advance!

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

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

发布评论

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

评论(2

相思故 2024-10-02 05:54:08

您可以使用Cache-Control max-age 相反,表示相对于响应时间的秒数:

实体的过期时间可以由源服务器使用 Expires 头指定(参见第 14.21 节)。或者,可以在响应中使用 max-age 指令来指定它。当缓存的响应中存在 max-age 缓存控制指令时,如果其当前年龄大于对该资源的新请求时给定的年龄值(以秒为单位),则该响应是过时的。

示例:

header('Cache-Control: max-age=28800');

请注意,如果 ExpiresCache-Controlmax-age 都存在,则 max-age > 优于 Expires

如果响应同时包含 Expires 标头和 max-age 指令,则 max-age 指令将覆盖 Expires 标头,即使 Expires 标头的限制性更强。

You can use Cache-Control’s max-age instead that indicated the number of second relative to the response time:

The expiration time of an entity MAY be specified by the origin server using the Expires header (see section 14.21). Alternatively, it MAY be specified using the max-age directive in a response. When the max-age cache-control directive is present in a cached response, the response is stale if its current age is greater than the age value given (in seconds) at the time of a new request for that resource.

An example:

header('Cache-Control: max-age=28800');

Note that if both Expires and Cache-Control’s max-age are present, max-age is preferred over Expires:

If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive.

纵山崖 2024-10-02 05:54:08

使用 strtotime 制作未来的时间戳,以及 gmdate 将其格式化为 GMT 时区的字符串。

define('EXPIRE_FORMAT', 'D, d M Y H:i:s T');
$expires = gmdate(EXPIRE_FORMAT, strtotime('+8 hours'));
header("Expires: $expires");

Use strtotime to make a timestamp in the future, and gmdate to format it as a string in the GMT timezone.

define('EXPIRE_FORMAT', 'D, d M Y H:i:s T');
$expires = gmdate(EXPIRE_FORMAT, strtotime('+8 hours'));
header("Expires: $expires");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文