不管你是使用php打开浏览器缓存还是使用apache,iis服务器环境来配置我们都是会针对浏览器的cache-control来操作,下面我来给大家介绍php设置图片浏览器缓存
cache-control
cache-control 是最重要的规则。这个字段用于指定所有缓存机制在整个请求/响应链中必须服从的指令。这些指令指定用于阻止缓存对请求或响应造成不利干扰的行为。这些指令通常覆盖默认缓存算法。缓存指令是单向的,即请求中存在一个指令并不意味着响应中将存在同一个指令。
cache-control 定义是:cache-control = “cache-control” “:” cache-directive。表 1 展示了适用的值。
表 1. 常用 cache-directive 值
cache-directive 说明
public 所有内容都将被缓存
private 内容只缓存到私有缓存中
no-cache 所有内容都不会被缓存
no-store 所有内容都不会被缓存到缓存或 internet 临时文件中
must-revalidation/proxy-revalidation 如果缓存的内容失效,请求必须发送到服务器/代理以进行重新验证
max-age=xxx (xxx is numeric) 缓存的内容将在 xxx 秒后失效, 这个选项只在http 1.1可用, 并如果和last-modified一起使用时, 优先级较高
在客户端通过浏览器发出第一次请求某一个url时,根据 http 协议的规定,浏览器会向服务器传送报头(http request header),服务器端响应同时记录相关属性标记(http reponse header),服务器端的返回状态会是200,格式类似如下:
代码如下 复制代码
http/1.1 200 ok
date: tue, 03 mar 2009 04:58:40 gmt
content-type: image/jpeg
content-length: 83185
last-modified: tue, 24 feb 2009 08:01:04 gmt
cache-control: max-age=2592000
expires: thu, 02 apr 2009 05:14:08 gmt
etag: “5d8c72a5edda8d6a:3239″
客户端第二次请求此url时,根据 http 协议的规定,浏览器会向服务器传送报头(http request header),服务器端响应并记录相关记录属性标记文件没有发生改动,服务器端返回304,直接从缓存中读取:
代码如下 复制代码
http/1.x 304 not modified
date: tue, 03 mar 2009 05:03:56 gmt
content-type: image/jpeg
content-length: 83185
last-modified: tue, 24 feb 2009 08:01:04 gmt
cache-control: max-age=2592000
expires: thu, 02 apr 2009 05:14:08 gmt
etag: “5d8c72a5edda8d6a:3239″
一、last-modified、expires和etag相关工作原理
1、last-modified
在浏览器第一次请求某一个url时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个last-modified的属性标记(http reponse header)此文件在服务期端最后被修改的时间,格式类似这样:
1 last-modified: tue, 24 feb 2009 08:01:04 gmt
客户端第二次请求此url时,根据 http 协议的规定,浏览器会向服务器传送 if-modified-since 报头(http request header),询问该时间之后文件是否有被修改过:
1 if-modified-since: tue, 24 feb 2009 08:01:04 gmt
如 果服务器端的资源没有变化,则自动返回 http 304 (not changed.)状态码,内容为空,这样就节省了传输数据量。当服务器端代码发生改变或者重启服务器时,则重新发出资源,返回和第一次请求时类似。从而 保证不向客户端重复发出资源,也保证当服务器有变化时,客户端能够得到最新的资源。
注:如果if-modified-since的时间比服务器当前时间(当前的请求时间request_time)还晚,会认为是个非法请求
2、etag工作原理
http 协议规格说明定义etag为“被请求变量的实体标记” (参见14.19)。
简单点即服务器响应时给请求url标记,并在http响应头中将其传送到客户端,类似服务器端返回的格式:
1 etag: “5d8c72a5edda8d6a:3239″
客户端的查询更新格式是这样的:
1 if-none-match: “5d8c72a5edda8d6a:3239″
如果etag没改变,则返回状态304。
即:在客户端发出请求后,http reponse header中包含 etag: “5d8c72a5edda8d6a:3239″
标识,等于告诉client端,你拿到的这个的资源有表示id:5d8c72a5edda8d6a:3239。
当下次需要发request索要同一个uri的时候,浏览器同时发出一个if-none-match报头( http request header)此时包头中信息包含上次访问得到的etag: “5d8c72a5edda8d6a:3239″标识。
1 if-none-match: “5d8c72a5edda8d6a:3239″
这样,client端等于cache了两份,服务器端就会比对2者的etag。如果if-none-match为false,不返回200,返回304 (not modified) response。
3、expires
给出的日期/时间后,被响应认为是过时。如expires: thu, 02 apr 2009 05:14:08 gmt
需和last-modified结合使用。用于控制请求文件的有效时间,当请求数据在有效期内时客户端浏览器从缓存请求数据而不是服务器端. 当缓存中数据失效或过期,才决定从服务器更新数据。
4、last-modified和expires
last-modified标识能够节省一点带宽,但是还是逃不掉发一个http请求出去,而且要和expires一起用。而expires标识却使得浏 览器干脆连http请求都不用发,比如当用户f5或者点击refresh按钮的时候就算对于有expires的uri,一样也会发一个http请求出去, 所以,last-modified还是要用的,而 且要和expires一起用。
5、etag和expires
如果服务器端同时设置了etag和expires时,etag原理同样,即与last-modified/etag对应的http request header:if-modified-since和if-none-match。我们可以看到这两个header的值和web server发出的last-modified,etag值完全一样;在完全匹配if-modified-since和if-none-match即检查 完修改时间和etag之后,服务器才能返回304.
6、last-modified和etag
last-modified 和etags请求的http报头一起使用,服务器首先产生 last-modified/etag标记,服务器可在稍后使用它来判断页面是否已经被修改,来决定文件是否继续缓存
过程如下:
1. 客户端请求一个页面(a)。
2. 服务器返回页面a,并在给a加上一个last-modified/etag。
3. 客户端展现该页面,并将页面连同last-modified/etag一起缓存。
4. 客户再次请求页面a,并将上次请求时服务器返回的last-modified/etag一起传递给服务器。
5. 服务器检查该last-modified或etag,并判断出该页面自上次客户端请求之后还未被修改,直接返回响应304和一个空的响应体。
注:
1、last-modified和etag头都是由web server发出的http reponse header,web server应该同时支持这两种头。
2、web server发送完last-modified/etag头给客户端后,客户端会缓存这些头;
3、客户端再次发起相同页面的请求时,将分别发送与last-modified/etag对应的http request header:if-modified-since和if-none-match。我们可以看到这两个header的值和web server发出的last-modified,etag值完全一样;
4、通过上述值到服务器端检查,判断文件是否继续缓存;
二、对于非实时交互动态页面中epires和etag处理
对数据更新并不频繁、如tag分类归档等等,可以考虑对其cache。简单点就是在非实时交互的动态程序中输出expires和etag标识,让其缓存。 但需要注意关闭session,防止http response时http header包含session id标识;
3.1、expires
如expires.php
代码如下 复制代码
header(’cache-control: max-age=86400,must-revalidate’);
header(’last-modified: ‘ .gmdate(’d, d m y h:i:s’) . ‘ gmt’ );
header(”expires: ” .gmdate (’d, d m y h:i:s’, time() + ‘86400′ ). ‘ gmt’);
3.2、etag
根据http返回状态来处理。当返回304直接从缓存中读取
如etag.php
代码如下 复制代码
cache();
echo date(”y-m-d h:i:s”);
function cache()
{
$etag = “http://longrujun.name”;
if (isset($_server['http_if_none_match']) &&
$_server['http_if_none_match'] == $etag)
{
header(’etag:’.$etag,true,304);
exit;
}
else header(’etag:’.$etag);
}
图片缓存实例
代码如下 复制代码
$imagepath = path/to/some/image;
$etag = $imagepath;
$etag .= filemtime($imagepath);
$etag = md5($etag);
if((isset($_server['http_if_none_match'])) &&
(stripslashes($_server['http_if_none_match']) == $etag)) {
header(http/1.1 304 not modified, true, 304);
exit();
}
header(etag: .$etag);
header(content-type: image/png);
readfile($imagepath);
exit();
上面的方法超级简单,下面看个完整实例
代码如下 复制代码
$fullpath = '/www/images/' . basename($_get['img']); //假定文件都在/www/images/下
if (!is_file($fullpath)) {
header(http/1.0 404 not found);
exit();
}
$info = getimagesize($fullpath); //获取图片信息
if (!$info) { //如果不是图片
header(http/1.0 404 not found);
exit();
}
// 以下凡是header函数都是在输出头部信息。较多。
header('content-type: '. $info['mime']); //类似于image/png
header('content-length: '. filesize($fullpath)); //文件长度
header('pragma: '); //没用,但要设置,防止服务器生成no-cache的可怕字眼
//手动设置过期时间,单位都是秒
$validtime = 48* 60 * 60; // 48小时
//缓存相对请求的时间,
header('cache-control: ' . 'max-age='. $validtime);
//也很重要的expires头,功能类似于max-age
//time()+$validtime: 设置期限,到期后才会向服务器提交请求
//gmdate,生成sun, 01 mar 2009 04:05:49 +0000 的字符串,而且是gmt标准时区
//preg_replace, 生成sun, 01 mar 2009 04:05:49 gmt, 注意:可能与服务器设置有关,
//但我都用默认设置
header('expires:'. preg_replace('/.{5}$/', 'gmt', gmdate('r', time()+ $validtime)));
//文件最后修改时间
$lasttime = filemtime($fullpath);
//最后修改时间,设置了,点击刷新时,浏览器再次请求图片才会发出'if_modified_since'头,
//从而被php程序读取
header('last-modified: ' . preg_replace('/.{5}$/', 'gmt', gmdate('r', $lasttime) ));
//重要,如果请求中的时间和 文件生成时间戳相等,则文件未修改,客户端可用缓存
if (strtotime($_server['http_if_modified_since']) == $lasttime) {
header(http/1.1 304 not modified); //服务器发出文件不曾修改的指令
exit();
}
//如果文件被修改了,只好重新发出数据
echo file_get_contents($fullpath);
http://www.bkjia.com/phpjc/631550.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631550.htmltecharticle不管你是使用php打开浏览器缓存还是使用apache,iis服务器环境来配置我们都是会针对浏览器的cache-control来操作,下面我来给大家介绍php设置图...
