PHP 会话修改缓存控制标头? [英] Cache-Control Header Modified By PHP Session?

查看:17
本文介绍了PHP 会话修改缓存控制标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 会话修改缓存控制标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆