PHP 是否可以设置为从不缓存响应,并始终向页面发送新内容?

发布于 2024-11-17 19:57:29 字数 214 浏览 4 评论 0原文

我有一个提取 XML 新闻提要的网页,但我不希望页面缓存它,以便显示的 XML 提要是最新的。

我已经尝试过此操作,因此缓存每 1800 秒重置一次:

$header[] = "Cache-Control: max-age=1800";

似乎不起作用。

那么我可以尝试如何让网页自动刷新 PHP 中的 XML 呢?

I have a webpage which pulls in an XML news feed and I don't want the page to cache it so that the XML feed displayed is most recent.

I tried this already so the cache is reset every 1800 seconds:

$header[] = "Cache-Control: max-age=1800";

Didn't seem to work.

So what could I try to make the webpage auto refresh the XML in the PHP?

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

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

发布评论

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

评论(5

睡美人的小仙女 2024-11-24 19:57:30

试试这个,

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

这将阻止页面缓存在用户的计算机上。

Try this,

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

This will prevent the page from being cached on a user's machine.

别把无礼当个性 2024-11-24 19:57:30

通常在 php 中我设置这些标头以避免缓存:

header( 'Cache-control: no-cache' );
header( 'Cache-control: no-store' , false );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );

但不清楚你在做什么。您是否正在调用输出 xml 的 php 页面?

在客户端,我通常为每个请求添加一个参数(&time=actualtimestamp),以便浏览器不会缓存它。

Usually in php I set these headers to avoid caching:

header( 'Cache-control: no-cache' );
header( 'Cache-control: no-store' , false );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );

But it is not clear what are you doing. Are you calling a php page that outputs xml?

Client-side, I usally add a parameter (&time=actualtimestamp) to every request so that the browser doesn't cache it.

心意如水 2024-11-24 19:57:30

如果可以,请执行 post 请求来获取 XML。 Post 请求不可缓存。

如果您无法执行发布请求,请在请求的查询信息部分添加一些唯一的内容
URI,就像当前时间:

old: http://exmaple.com/data.xml
new: http://exmaple.com/data.xml?878621387
                                  ^ random number

我首先读错了你的问题,并担心你想阻止你的响应被缓存。也许它仍然有用,所以我把它留在这里:

在流行的应用程序中,经常存在设置一堆标头以防止缓存的函数。这是来自 WordPress 代码库的示例:

  function nocache_headers() {
      @ header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
      @ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
      @ header('Cache-Control: no-cache, must-revalidate, max-age=0');
      @ header('Pragma: no-cache');
  }

它设置 标头过期到过去的日期。因此,在交付时,文档已经过期或在缓存方面已过时。这些文档不应该通过缓存传递。

然后是 Last-Modified 标题设置为当前时间。

然后 Cache-Control 标头设置为缓存 不应缓存响应,必须重新验证,并应保留最大年龄为 0。这听起来可能有点精神分裂(就好像没有缓存,不可能有年龄),但这只是用数据淹没代理和用户代理以使它们停止。

最后是 Pragma 标头设置为 no-cache 以防止 HTTP/1.0 兼容代理和用户代理的缓存(HTTP/1.1 客户端通常使用缓存控制)。

所有这些标头在 超文本传输​​协议 -- HTTP/1.1 RFC 2616 中有详细解释菲尔丁等人。如果您渴望查看可用的选项,请参阅第 14 节标题字段定义

If you can, do a post request to obtain the XML. Post requests are not cache-able.

If you can not do a post request, add something unique to the query-info part the request
URI, like the current time:

old: http://exmaple.com/data.xml
new: http://exmaple.com/data.xml?878621387
                                  ^ random number

I first read your question wrong and was concerned about that you want to prevent that a response of yours is being cached. Maybe it's still of use, so I leave it here:

In popular apps there often exists functions that set a bunch of headers to prevent caching. This is an example from the wordpress codebase:

  function nocache_headers() {
      @ header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
      @ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
      @ header('Cache-Control: no-cache, must-revalidate, max-age=0');
      @ header('Pragma: no-cache');
  }

It set the Expires header to a date back in the past. So while being delivered, the document already expired or in caching terms is stale. Those documents should not be delivered by caches.

Then the Last-Modified header is set to the current time.

Then the Cache-Control header is set to signal that caches should not cache the response, must revalidate and should keep copies for a maximum age of 0. This sounds a bit schizophrenic maybe (as if not cached, there can't be an age), but that's just to flood proxies and user-agents with data to make them stop.

Finally the Pragma header is set to no-cache to prevent caching as well for HTTP/1.0 compatible proxies and user-agents (HTTP/1.1 clients normally make use of Cache-Control).

All these headers are explained in detail in the Hypertext Transfer Protocol -- HTTP/1.1 RFC 2616 Fielding, et al. Section 14 Header Field Definitions if you're eager to checkout the options available.

昵称有卵用 2024-11-24 19:57:30

您如何请求 XML 文件?可能是使用 cURL 或 url fopen,对吗?

除非您明确要求,否则这两种方法都不会缓存响应。所以你应该没问题。

How are you requesting the XML file? Probably either with cURL or url fopen, right?

Both these methods do not cache the response unless you specifically explicitly ask them to. So you should be fine.

国际总奸 2024-11-24 19:57:30

好吧,你可能不必在 php/perl/.. 中设置它。你也可以使用 htaccess 或 apache 配置。我用于 htaccess 的一个示例: http:// snipplr.com/view/4265/cache-control-with-htaccess-expires-by-type/

Well probably you have not to set it in php/perl/.. you can also use the htaccess or apache configuration. One example i am using for htaccess: http://snipplr.com/view/4265/cache-control-with-htaccess-expires-by-type/

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