問題描述
我正在使用 Zend_Controller_Response
對象將圖像輸出到瀏覽器.我打算對圖像應用緩存,但是某些原因導致 Cache-Control 標頭被覆蓋.
我的代碼如下:
$this->getResponse()->setHeader('Last-Modified', $modifiedTime, true)->setHeader('ETag', md5($modifiedTime), true)->setHeader('Expires', $expires, true)->setHeader('Pragma', '', true)->setHeader('緩存控制', 'max-age=3600')->setHeader('Content-Type', $mimeType, true)->setHeader('Content-Length', $size, true)->setBody($data);
輸出(在 Firebug 中查看)是:
響應頭
<塊引用>- 日期
- 格林威治標準時間 2009 年 3 月 25 日星期三 10:34:40
- 服務器
- 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
- Etag
- d3ef646c640b689b??0101f3e03e08a524
- 內容長度
- 1452
- X-UA-Compatible
- IE=EmulateIE7
- X-Robots-Tag
- noindex
- Keep-Alive
- timeout=15, max=100
- 連接
- Keep-Alive
- 內容類型
- 圖像/jpeg
請求標頭
<塊引用>- 主機
- 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,deflate
- Accept-Charset
- ISO-8859-1,utf-8;q=0.7,*;q=0.7
- 保持活動
- 300
- 連接
- 保持連接
- 推薦人
- http://khall.####.###.######.com/
- Cookie
- PHPSESSID=abf5056e1289d3010448107632a1c1bd
如您所見,緩存控件被修改為包括:
<塊引用>no-store, no-cache, must-revalidate, post-check=0, pre-check=0
我懷疑是針對請求中發送的會話 cookie.有人知道發送我需要的標頭的方法,但仍將會話保留在請求中嗎?我的應用程序通過引導程序運行,會話使用 Zend_Session 處理.
任何幫助將不勝感激.
來自 Zend_Controller 文檔,部分 10.9.響應對象
<塊引用>setHeader($name, $value, $replace =false) 用于設置個人標題.默認情況下,它不替換相同的現有標題對象中的名稱;然而,設置$replace 為 true 將強制它執行所以.
您遇到的問題是您的 max-age=3600 被附加到緩存控制標頭,而不是替換它.嘗試將 $replace 參數設置為 true.
I'm outputting an image to the browser using a Zend_Controller_Response
object. It is my intention to apply caching to the image, however something is causing the Cache-Control header to be overwritten.
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:
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
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
As you can see, the cache control is modified to include:
no-store, no-cache, must-revalidate, post-check=0, pre-check=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.
Any help would be appreciated.
From the Zend_Controller documentation, section 10.9. The Response Object
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.
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 $replace parameter to true.
這篇關于PHP 會話修改緩存控制標頭?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!