PHP 会话修改缓存控制标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/681115/
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
Cache-Control Header Modified By PHP Session?
提问by Kieran Hall
I'm outputting an image to the browser using a Zend_Controller_Responseobject. It is my intention to apply caching to the image, however something is causing the Cache-Control header to be overwritten.
我正在使用Zend_Controller_Response对象将图像输出到浏览器。我打算对图像应用缓存,但是有些事情导致 Cache-Control 标头被覆盖。
My code is as follows:
我的代码如下:
$this->getResponse()
->setHeader('Last-Modified', $modifiedTime, true)
->setHeader('ETag', md5($modifiedTime), true)
->setHeader('Expires', $expires, true)
->setHeader('Pragma', '', true)
->setHeader('Cache-Control', 'max-age=3600')
->setHeader('Content-Type', $mimeType, true)
->setHeader('Content-Length', $size, true)
->setBody($data);
The output (as viewed in Firebug) is:
输出(在 Firebug 中查看)是:
Response Headers
响应头
- Date
- Wed, 25 Mar 2009 10:34:40 GMT
- Server
- Apache/2.2.3 (Ubuntu) mod_ssl/2.2.3 OpenSSL/0.9.8c
- Expires
- Thu, 26 Mar 2009 10:34:41 GMT
- Cache-Control
- no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=3600
- Last-Modified
- 1234872514
- Etag
- d3ef646c640b689b0101f3e03e08a524
- Content-Length
- 1452
- X-UA-Compatible
- IE=EmulateIE7
- X-Robots-Tag
- noindex
- Keep-Alive
- timeout=15, max=100
- Connection
- Keep-Alive
- Content-Type
- image/jpeg
- 日期
- 2009 年 3 月 25 日,星期三 10:34:40 GMT
- 服务器
- Apache/2.2.3 (Ubuntu) mod_ssl/2.2.3 OpenSSL/0.9.8c
- 过期
- 2009 年 3 月 26 日星期四 10:34:41 GMT
- 缓存控制
- no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=3600
- 最后修改
- 1234872514
- 标签
- d3ef646c640b689b0101f3e03e08a524
- 内容长度
- 1452
- X-UA-兼容
- IE=模拟IE7
- X-Robots-Tag
- 无索引
- 活着
- 超时=15,最大=100
- 联系
- 活着
- 内容类型
- 图像/JPEG
Request Headers
请求头
- Host
- khall.####.###.######.com
- User-Agent
- Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030422 Ubuntu/8.04 (hardy) Firefox/3.0 .7
- Accept
- text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Language
- en-gb,en;q=0.5
- Accept-Encoding
- gzip,deflate
- Accept-Charset
- ISO-8859-1,utf-8;q=0.7,*;q=0.7
- Keep-Alive
- 300
- Connection
- keep-alive
- Referer
- http://khall.####.###.######.com/
- Cookie
- PHPSESSID=abf5056e1289d3010448107632a1c1bd
- 主持人
- khall.####.###.######.com
- 用户代理
- Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030422 Ubuntu/8.04 (hardy) Firefox/3.0 .7
- 接受
- text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- 接受语言
- en-gb,en;q=0.5
- 接受编码
- gzip,放气
- 接受字符集
- ISO-8859-1,utf-8;q=0.7,*;q=0.7
- 活着
- 300
- 联系
- 活着
- 推荐人
- http://khall.####.###.######.com/
- 曲奇饼
- PHPSESSID=abf5056e1289d3010448107632a1c1bd
As you can see, the cache control is modified to include:
如您所见,缓存控件被修改为包括:
no-store, no-cache, must-revalidate, post-check=0, pre-check=0
无存储、无缓存、必须重新验证、后检查=0、预先检查=0
My suspicion is towards the session cookie being sent in the request. Does anybody know a way to send the header that I require, yet still keep the session in the request? My application is run through a bootstrap, and sessions are handled using Zend_Session.
我怀疑是针对请求中发送的会话 cookie。有人知道发送我需要的标头的方法,但仍将会话保留在请求中吗?我的应用程序通过引导程序运行,会话使用 Zend_Session 处理。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Stefan Gehrig
You're right by assuming that this behaviour is connected to the session mechanism in PHP. There is a configuration setting session.cache_limiterthat controls the caching HTTP headers that will be sent with the response. The default setting here is nocachewhich sends
假设此行为与 PHP 中的会话机制相关,您是对的。有一个配置设置session.cache_limiter控制将与响应一起发送的缓存 HTTP 标头。这里的默认设置是nocache发送
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
You overwrite all of these headers within your controller besides the Cache-Control-header (you only append your max-age=3600setting here).
除了Cache-Control-header之外,您还可以覆盖控制器中的所有这些标头(您只max-age=3600在此处附加您的设置)。
Possible solutions are:
可能的解决方案是:
- changing the PHP configuration (
session.cache_limiter) to e.g.none- but this could introduce problems to other PHP applications - set the
session.cache_limiteron each request usingsession_cache_limiter() - overwrite the full
Cache-Control-header in your controller with the designated string
- 将 PHP 配置 (
session.cache_limiter)更改为 egnone- 但这可能会给其他 PHP 应用程序带来问题 - 所设定的
session.cache_limiter每个请求使用session_cache_limiter() Cache-Control用指定的字符串覆盖控制器中的完整标头
The possible values for session.cache_limiterand session_cache_limiter()are:
对于可能的值session.cache_limiter和session_cache_limiter()有:
none: no header will be sent
none: 不发送标头
nocache:
无缓存:
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
private:
私人:
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, max-age=10800, pre-check=10800
private_no_expire:
private_no_expire:
Cache-Control: private, max-age=10800, pre-check=10800
public:
公众:
Expires: pageload + 3 hours
Cache-Control: public, max-age=10800
回答by karim79
From the Zend_Controller documentation, section 10.9. The Response Object
来自 Zend_Controller 文档的10.9节。响应对象
setHeader($name, $value, $replace = false) is used to set an individual header. By default, it does not replace existing headers of the same name in the object; however, setting $replace to true will force it to do so.
setHeader($name, $value, $replace = false) 用于设置单个标题。默认情况下,它不会替换对象中现有的同名标题;但是,将 $replace 设置为 true 将强制它这样做。
The problem you are having is your max-age=3600 is being appended to the cache-control header, as opposed to replacing it. Try setting the $replaceparameter to true.
您遇到的问题是您的 max-age=3600 被附加到缓存控制标头,而不是替换它。尝试将$replace参数设置为true。

