什么可以添加“Pragma:no-cache”?我的回复标题? (阿帕奇、PHP)

发布于 2024-09-05 23:03:27 字数 479 浏览 3 评论 0原文

我有一个由我继承维护的网站,这是一个大混乱。
我正在做的事情之一就是提高性能。除此之外,我还向图像添加 Expires 标头。

现在,有一些图像是通过 PHP 文件提供的,我注意到它们确实有 Expires 标头,但它们每次也会加载。

查看响应标头,我看到了这一点:

Expires Wed, 15 Jun 2011 18:11:55 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache

这显然解释了问题。

现在,我已经查看了整个代码库,并且它没有在任何地方说“pragma”。 .htaccess 似乎也没有任何相关内容。

有什么想法可以设置这些“pragma”(和“缓存控制”)标头,以及如何避免它?

I have a website which maintenance I've inherited, which is a big hairy mess.
One of the things I'm doing is improving performance. Among other things, I'm adding Expires headers to images.

Now, there are some images that are served through a PHP file, and I notice that they do have the Expires header, but they also get loaded every time.

Looking at Response Headers, I see this:

Expires Wed, 15 Jun 2011 18:11:55 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache

Which obviously explains the problem.

Now, I've looked all over the code base, and it doesn't say "pragma" anywhere. .htaccess doesn't seem to have anything related either.

Any ideas what could be setting those "pragma" (and "cache-control") headers, and how can I avoid it?

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

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

发布评论

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

评论(7

月亮邮递员 2024-09-12 23:03:27

罪魁祸首可能是 php.ini,其中 session.cache_limiter=nocache。将值更改为空白或公共以避免反缓存标头。

The culprit may be php.ini, where session.cache_limiter=nocache. Change the value to blank or public to avoid the anti-cacheing headers.

何其悲哀 2024-09-12 23:03:27

session_start(); 之前,我对 Pragma: nocache session_cache_limiter(false); 也有类似的问题,

似乎抑制了它。

I had a similar problem with Pragma: nocache

session_cache_limiter(false); prior to session_start(); seemed to suppress it.

岁吢 2024-09-12 23:03:27

创建一个简单的文件,该文件不包含任何 PHP 库,但与通过 PHP 文件提供图像的文件位于同一文件夹中。

file: test.php

通过浏览器请求该文件并检查标头。如果您看到不想要的响应标头,您就知道它们是通过 apache 配置的,而不是通过 PHP 文件生成的,您可以将搜索集中在目录树中的 .htaccess 文件和 http.confg 上和其他包含的 apache 配置文件。您需要搜索

<Directory....

可能

<VirtualHost

适用于您的网站的部分。

如果您在该简单 PHP 文件的请求中没有看到标头,则说明 PHP 正在某处设置标头。在图像服务文件的末尾(或在它回显图像并退出之后),但以下 PHP 片段)

var_dump(get_included_files());

通过图像服务 URL 请求图像。上面的代码片段将打印出请求中使用的所有 PHP 文件。 (您可能需要查看源代码或使用curl来查看原始输出,因为浏览器将报告无效图像)

将文件的子集设置为工作文件,在它们中搜索对函数的调用

header();

header 函数是原始 PHP 代码设置响应标头的唯一方法(我认为)。您还需要搜索

call_user_func
eval
$

页面上是否存在使用 PHP 元编程功能来调用 header 函数的动态代码。

祝你好运!

Create a simple file that includes none of your PHP libraries but lives in the same folder as the file that serves up your images through a PHP file.

file: test.php

Request this file through a browser and check the headers. If you see the Response headers that you don't want, you know that they're configured via apache and not generated via a PHP file and you can concentrate your searches on .htaccess file in the directory tree, and on the http.confg and other included apache config files. You'll want to search for

<Directory....

and

<VirtualHost

sections that may apply to your site.

If you don't see the headers in a request for that simple PHP file, you know that PHP is setting the headers somewhere. At the end of your image serving file (or right after it echos the image and exits), but the following PHP snippet)

var_dump(get_included_files());

Request an image through the image serving URL. That above snippet will print out all the PHP files used in the request. (you'll probably need to view source or use curl to see the raw output, as the browser will report an invalid image)

Having a subset of your files to work file, search through them for calls to the

header();

function. The header function is the only way (I think) that raw PHP code can set Response headers. You'll also want to search for

call_user_func
eval
$

in case there's any dynamic code on the page that's using PHP's meta-programming capabilities to call the header function.

Good luck!

关于从前 2024-09-12 23:03:27

尝试取消 .htaccess 中的标头设置。下面的示例将为所有匹配扩展名 icojpegpnggif的文件取消设置它们>jscss

<FilesMatch "\.(ico|jpeg|png|gif|js|css)$">
    Header unset Cache-Control
    Header unset Pragma
</FilesMatch>

您可以在 this 中找到一些提示文章

Try unsetting the headers in .htaccess. The below example will unset them for all files matching the extensions ico, jpeg, png, gif, js, css:

<FilesMatch "\.(ico|jpeg|png|gif|js|css)
quot;>
    Header unset Cache-Control
    Header unset Pragma
</FilesMatch>

You can find some hints in this article.

呆萌少年 2024-09-12 23:03:27

我在运行时使用以下命令执行此操作:

header("Pragma:");

这强制脚本取消设置 Pragma 标头。

I did this at runtime with this:

header("Pragma:");

which forced the script to unset the Pragma header.

伪装你 2024-09-12 23:03:27

如果它不在 .htaccess 中,它可能位于主 Apache 配置文件中 - 或其包含文件之一,具体取决于设置。

If it's not in .htaccess it may be in the main Apache config file - or one of its includes, depending on the setup.

对风讲故事 2024-09-12 23:03:27

对于有类似问题的人来说,值得注意的是,许多框架会自动添加标头,尤其是缓存标头。在框架库或应用程序中重载它们相当容易。

It's worth noting for people with similar problems that many frameworks will auto-add headers especially caching ones. It's fairly easy to overload them either in the framework library or within your app.

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