关闭

关于Etag/If-None-Match的用法

标签: etag服务器windows浏览器serveraction
10961人阅读 评论(0) 收藏 举报
分类:

关于Last-Modified/If-Modified-Since及Etag/If-None-Match的用法

相关代码
if (method_exists($controller, $action)) {
         ob_start();
            $controller -> {$action}();
            $content = ob_get_contents();
            ob_end_clean();
                       
            $etag = md5($content); //计算输出内容的md5
            fheader('Etag: ' . $etag); //服务器端输出Etag header
   //判断浏览器发送过来的If-None-Match头与服务器端的etag值是否相等
            if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
             write_log(sprintf("%s Client HTTP_IF_NONE_MATCH:|%s| and Server Etag:|%s| is same, send 304 header!", __METHOD__, $_SERVER['HTTP_IF_NONE_MATCH'], $etag));
             fheader('HTTP/1.1 304 Not Modified!'); //相等,直接向浏览器发送304状态码,浏览器则直接取本地的缓存数据,节省带宽,提高页面响应速度
            } else {
             write_log(sprintf("%s file content already modified, output content!", __METHOD__));
             echo $content;
            }
        } else {
            $error = get_class($controller) . "::$action not exists!";
            die($error);
            write_log($error);
        }

 

注意服务器端(php)接收浏览器发送过来的If-Modified-Since及If-None-Match时服务器端存在变量
$_SERVER['HTTP_IF_MODIFIED_SINCE']及$_SERVER['HTTP_IF_NONE_MATCH']

---------------------------------------------------------
http://hi.baidu.com/kakaying1985/blog/item/286a13ca90774882c817685d.html
Last-Modified和Etags如何帮助提高性能?
         聪明的开发者会把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和一个空的响应体。
---------------------------------------------------------

-------------------------------------------
下面是用firebug监控到的

Request Headers
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language zh-cn,zh;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Cookie 61m_auth=97S0CrUoaHtE8qlkgA0hMJjzgFQpXZl1r6HBCbj8OoreWZwoeysL%2FLedoA; 61m_cookietime=2592000; 61m_sid=6jgxcA
If-Modified-Since Tue, 19 Aug 2008 06:49:35GMT
If-None-Match 7936caeeaf6aee6ff8834b381618b513
Cache-Control max-age=0

Response Headers
Date Tue, 19 Aug 2008 06:50:19 GMT
Server Apache/2.0.47 (Win32) PHP/5.2.3
X-Powered-By PHP/5.2.3
Expires Tue, 19 Aug 2008 07:00:19 GMT
Last-Modified Tue, 19 Aug 2008 06:49:35GMT
Etag 7936caeeaf6aee6ff8834b381618b513
Content-Encoding gzip
Vary Accept-Encoding
Content-Length 421
Content-Type text/html; charset=utf-8
-------------------------------------------

-------------------------------------------
下面是用Http look监控到的内容,说明304头已经生效了
Request
GET /i33edu_v2/web/test/display_group HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: 61m_auth=97S0CrUoaHtE8qlkgA0hMJjzgFQpXZl1r6HBCbj8OoreWZwoeysL%2FLedoA; 61m_cookietime=2592000; 61m_sid=6jgxcA
If-Modified-Since: Tue, 19 Aug 2008 06:49:35GMT
If-None-Match: 7936caeeaf6aee6ff8834b381618b513
Cache-Control: max-age=0

Response
HTTP/1.1 304 Not Modified!
Date: Tue, 19 Aug 2008 06:50:19 GMT
Server: Apache/2.0.47 (Win32) PHP/5.2.3
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Etag: 7936caeeaf6aee6ff8834b381618b513
Expires: Tue, 19 Aug 2008 07:00:19 GMT
Vary: Accept-Encoding

000000  1F 8B 08 00 00 00 00 00  00 03 D2 D5 25 19 D8 24  .?.......艺%.?
000010  15 29 E8 DB 71 A9 94 25  16 29 64 16 2B 24 2A 14  .)?q%.)d.+$*.
000020  97 14 65 E6 A5 EB 28 14  00 A9 12 85 C4 62 A8 00  ?e?.(..?吥b?
000030  54 5D 51 6A 62 8A 42 5A  51 7E AE 42 72 62 72 46  T]Qjb夿ZQ~瓸rbrF
000040  AA 95 0D 50 36 3F 2F DD  AE A6 C6 46 1F CA 54 80  獣.P6?/莓ζF.蔜?BR>000050  2A 25 DD 2D 30 C7 00 00  00 00 FF FF 03 00 4F BC  *%?0?...?..000060  32 45 C7 00 00 00                                 2E?..
O?
-------------------------------------------

-------------------------------------------
下面是用ie7的http watch监控的

GET /i33edu_v2/web/test/display_group HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
If-Modified-Since: Tue, 19 Aug 2008 07:09:35 GMT
If-None-Match: 7936caeeaf6aee6ff8834b381618b513
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; POTU(RR:27120522:0:5231843); Potu-WebBrowser www.potu.com; CIBA)
Host: localhost
Connection: Keep-Alive
Cookie: 61m_sid=nQbrC8

HTTP/1.1 304 Not Modified!
Date: Tue, 19 Aug 2008 07:10:47 GMT
Server: Apache/2.0.47 (Win32) PHP/5.2.3
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Etag: 7936caeeaf6aee6ff8834b381618b513
Expires: Tue, 19 Aug 2008 07:20:47 GMT
Vary: Accept-Encoding

\u001F?
-------------------------------------------

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:271996次
    • 积分:3261
    • 等级:
    • 排名:第10417名
    • 原创:34篇
    • 转载:123篇
    • 译文:1篇
    • 评论:45条
    最新评论