<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>超群.com的博客 &#187; Etag</title>
	<atom:link href="http://www.fuchaoqun.com/tag/etag/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP处理Etag、lastModified和Expires</title>
		<link>http://www.fuchaoqun.com/2009/09/php_etag_lastmodified_expires/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php_etag_lastmodified_expires</link>
		<comments>http://www.fuchaoqun.com/2009/09/php_etag_lastmodified_expires/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 13:40:12 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Etag]]></category>
		<category><![CDATA[Expires]]></category>
		<category><![CDATA[lastModified]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=237</guid>
		<description><![CDATA[之前看到robbin基于资源的HTTP Cache的实现介绍，想到这是一个很有意思的功能，原理很简单，但很多人都会忽略，于是乎打算集成到ColaPHP框架中来，让浏览器缓存动态内容，对于一些由动态脚本生成、更新不频繁但又会被用户重复访问的页面内容，还是很有意义的。 如果在服务器端设置了Etag、lastModified和Expires之后，下次再访问同一资源的时候，一个典型的HTTP头是这样的： Host            127.0.0.1 User-Agent        Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 QQDownload/1.7 Accept            text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 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 If-Modified-Since    Sat, 05 Sep 2009 12:44:56 GMT If-None-Match        foobar Cache-Control        max-age=0 关于lastModified、Etag和Expires的工作原理，可以参看http://longrujun.name/index.php/2009/03/04/etag%E5%92%8Cexpires/，简单来说： lastModified：设定一个最后修改时间，浏览器下次访问的时候，发送一个&#8221;If-Modified-Sinc&#8221;的头信息，如果内容在这个时间之后没有更新，服务器直接返回一个304 Not [...]]]></description>
			<content:encoded><![CDATA[<p>之前看到robbin<a href="http://robbin.javaeye.com/blog/462476" target="_blank">基于资源的HTTP Cache的实现介绍</a>，想到这是一个很有意思的功能，原理很简单，但很多人都会忽略，于是乎打算集成到<a href="http://code.google.com/p/colaphp/" target="_blank">ColaPHP</a>框架中来，让浏览器缓存动态内容，对于一些由动态脚本生成、更新不频繁但又会被用户重复访问的页面内容，还是很有意义的。</p>
<p>如果在服务器端设置了Etag、lastModified和Expires之后，下次再访问同一资源的时候，一个典型的HTTP头是这样的：</p>
<pre>Host            127.0.0.1
User-Agent        Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 QQDownload/1.7
Accept            text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
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
If-Modified-Since    Sat, 05 Sep 2009 12:44:56 GMT
If-None-Match        foobar
Cache-Control        max-age=0</pre>
<p>关于lastModified、Etag和Expires的工作原理，可以参看<a href="http://longrujun.name/index.php/2009/03/04/etag%E5%92%8Cexpires/" target="_blank">http://longrujun.name/index.php/2009/03/04/etag%E5%92%8Cexpires/</a>，简单来说：</p>
<p><strong>lastModified</strong>：设定一个最后修改时间，浏览器下次访问的时候，发送一个&#8221;If-Modified-Sinc&#8221;的头信息，如果内容在这个时间之后没有更新，服务器直接返回一个304 Not Modified而不传输详细内容，可以节省带宽。</p>
<p><strong>Etag</strong>：设定一个标记，浏览器下次访问时，发送一个&#8221;If-None-Match&#8221;的头信息，如果服务器内容还是这个标记没变，也直接返回一个304 Not Modified而不传输详细内容，同样可以节省带宽。</p>
<p><strong>Expires</strong>：设定一个过期时间，如果当前请求在这个过期时间之类，则<strong>不发送HTTP请求</strong>，不仅可以节约服务器带宽，还可以<strong>减少服务器HTTP请求数</strong>。</p>
<p>主要通过header函数来发送，比较简单，直接上代码：</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> etag<span style="color: #009900;">&#40;</span><span style="color: #000088;">$etag</span><span style="color: #339933;">,</span> <span style="color: #000088;">$notModifiedExit</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$notModifiedExit</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_IF_NONE_MATCH'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$etag</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_IF_NONE_MATCH'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">statusCode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'304'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Etag: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$etag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> lastModified<span style="color: #009900;">&#40;</span><span style="color: #000088;">$modifiedTime</span><span style="color: #339933;">,</span> <span style="color: #000088;">$notModifiedExit</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$modifiedTime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D, d M Y H:i:s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modifiedTime</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' GMT'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$notModifiedExit</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_IF_MODIFIED_SINCE'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$modifiedTime</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_IF_MODIFIED_SINCE'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">statusCode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'304'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last-Modified: <span style="color: #006699; font-weight: bold;">$modifiedTime</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> expires<span style="color: #009900;">&#40;</span><span style="color: #000088;">$seconds</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1800</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D, d M Y H:i:s'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$seconds</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' GMT'</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: <span style="color: #006699; font-weight: bold;">$time</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>如果你是用<a href="http://code.google.com/p/colaphp/" target="_blank">ColaPHP</a>，可以直接在controller里面写上：</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 设定最后修改时间，通常是数据库中内容修改时间</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastModified</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1252154696</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 设定内容标记，自己可以按照一定的规则来生成，当然也可以用内容最后修改的时间戳</span>
 <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">etag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foobar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 设定失效时间</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expires</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>PS：ColaPHP近期发0.3alpha，主要改进Model和DB设计，加入了一下小功能（比如验证码），以及bugfix。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2009/09/php_etag_lastmodified_expires/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
