什么可以将“Pragma:no-cache”添加到我的响应标头中?(阿帕奇,PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3047902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What could be adding "Pragma:no-cache" to my response Headers? (Apache, PHP)
提问by Daniel Magliola
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 Expiresheaders to images.
我有一个网站,我继承了它的维护,这是一个大毛茸茸的烂摊子。
我正在做的一件事是提高性能。除其他外,我正在Expires为图像添加标题。
Now, there are some images that are served through a PHP file, and I notice that they do have the Expiresheader, but they also get loaded every time.
现在,有一些图像是通过 PHP 文件提供的,我注意到它们确实有Expires标题,但它们也每次都被加载。
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.
现在,我已经查看了所有代码库,它在任何地方都没有说“pragma”。.htaccess 似乎也没有任何相关性。
Any ideas what could be setting those "pragma" (and "cache-control") headers, and how can I avoid it?
任何想法可以设置那些“pragma”(和“cache-control”)标头,我该如何避免它?
采纳答案by Alan Storm
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.
创建一个不包含任何 PHP 库但与通过 PHP 文件提供图像的文件位于同一文件夹中的简单文件。
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
通过浏览器请求此文件并检查标题。如果您看到不想要的响应头,您就知道它们是通过 apache 配置的,而不是通过 PHP 文件生成的,您可以将搜索集中在目录树中的 .htaccess 文件和 http.confg和其他包含的 apache 配置文件。你会想要搜索
<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)
如果您在对那个简单 PHP 文件的请求中没有看到标头,那么您就知道 PHP 正在某处设置标头。在图像服务文件的末尾(或在它回显图像并退出之后),但以下 PHP 代码段)
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)
通过图像服务 URL 请求图像。上面的代码段将打印出请求中使用的所有 PHP 文件。(您可能需要查看源代码或使用 curl 来查看原始输出,因为浏览器会报告无效图像)
Having a subset of your files to work file, search through them for calls to the
将文件的子集作为工作文件,搜索它们以查找对
header();
function. The headerfunction is the only way (I think) that raw PHP code can set Response headers. You'll also want to search for
功能。该header函数是(我认为)原始 PHP 代码可以设置 Response 标头的唯一方法。您还想搜索
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 headerfunction.
以防页面上有任何动态代码使用 PHP 的元编程功能来调用该header函数。
Good luck!
祝你好运!
回答by technomage
The culprit may be php.ini, where session.cache_limiter=nocache. Change the value to blank or public to avoid the anti-cacheing headers.
罪魁祸首可能是 php.ini,其中 session.cache_limiter=nocache。将值更改为空白或公共以避免反缓存标头。
回答by Mahks
I had a similar problem with Pragma: nocache
我有一个类似的问题 Pragma: nocache
session_cache_limiter(false);prior to session_start();seemed to suppress it.
session_cache_limiter(false);之前session_start();似乎压制了它。
回答by BalusC
Try unsetting the headers in .htaccess. The below example will unset them for all files matching the extensions ico, jpeg, png, gif, js, css:
尝试取消设置.htaccess. 下面的示例将为匹配扩展名ico, jpeg, png, gif, js, 的所有文件取消设置它们css:
<FilesMatch "\.(ico|jpeg|png|gif|js|css)$">
Header unset Cache-Control
Header unset Pragma
</FilesMatch>
You can find some hints in this article.
您可以在本文中找到一些提示。
回答by Michael Butler
I did this at runtime with this:
我在运行时这样做了:
header("Pragma:");
which forced the script to unset the Pragma header.
这迫使脚本取消设置 Pragma 标头。
回答by Colonel Sponsz
If it's not in .htaccess it may be in the main Apache config file - or one of its includes, depending on the setup.
如果它不在 .htaccess 中,则它可能在主 Apache 配置文件中 - 或其包含之一,具体取决于设置。
回答by twunde
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.
对于有类似问题的人来说值得注意的是,许多框架会自动添加标头,尤其是缓存标头。在框架库或应用程序中重载它们相当容易。

