php中header函数参数的Cache-control:private,no-cache,must

<p>由于我做的是登录,也就是用户登录每次登陆时都要来访问服务器,不需要在客户机做缓存,于是在网上找了找,发下一下文章不错于是传了上来。</p><p>网页的缓存是由HTTP消息头中的&quot;Cache-control&quot;来控制的,常见的取值有private、no-cache、max-age、must-revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:</p><p>(1) 打开新窗口</p><p>值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。</p><p>而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:</p><p>Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器)</p><p>(2) 在地址栏回车</p><p>值为private或must-revalidate则只有第一次访问时会访问服务器,以后就不再访问。</p><p>值为no-cache,那么每次都会访问。</p><p>值为max-age,则在过期之前不会重复访问。</p><p>(3) 按后退按扭</p><p>值为private、must-revalidate、max-age,则不会重访问,</p><p>值为no-cache,则每次都重复访问</p><p>(4) 按刷新按扭</p><p>无论为何值,都会重复访问</p><p>Cache-control值为&quot;no-cache&quot;时,访问此页面不会在Internet临时文章夹留下页面备份。</p><p>另外,通过指定&quot;Expires&quot;值也会影响到缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问: Expires: Fri, 31 Dec 1999 16:00:00 GMT</p><p>比如:禁止页面在IE中缓存</p><p>http响应消息头部设置:</p><pre class="brush:php;toolbar:false">CacheControl=no-cache Pragma=no-cache Expires=-1</pre><p>Expires是个好东东,如果服务器上的网页经常变化,就把它设置为-1,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。</p><p>当HTTP1.1服务器指定CacheControl = no-cache时,浏览器就不会缓存该网页。</p><p>旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。</p><p>所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。</p><p>如果客户端通过安全连接 (https://)/与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,</p><p>则 Internet Explorer不会缓存此响应。注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1相同,该页将被缓存,但被标记为立即过期</p><p>header常用指令</p><p>header分为三部分:</p><p>第一部分为HTTP协议的版本(HTTP-Version);</p><p>第二部分为状态代码(Status);</p><p>第三部分为原因短语(Reason-Phrase)。</p><pre class="brush:php;toolbar:false">//fix404pages:用这个header指令来解决URL重写产生的404header header(&#39;HTTP/1.1200OK&#39;); //set404header:页面没找到 header(&#39;HTTP/1.1404NotFound&#39;); //页面被永久删除,可以告诉搜索引擎更新它们的urls //setMovedPermanentlyheader(goodforredrictions) //usewithlocationheader header(&#39;HTTP/1.1301MovedPermanently&#39;); //访问受限 header(&#39;HTTP/1.1403Forbidden&#39;); //服务器错误 header(&#39;HTTP/1.1500InternalServerError&#39;); //重定向到一个新的位置 //redirecttoanewlocation: header(&#39;Location:http://www.sina.com.cn); 延迟一段时间后重定向 //redrictwithdelay: header(&#39;Refresh:10;url=http://www.sina.com.cn&#39;); print&#39;Youwillberedirectedin10seconds&#39;; //覆盖X-Powered-Byvalue //overrideX-Powered-By:PHP: header(&#39;X-Powered-By:PHP/4.4.0&#39;); header(&#39;X-Powered-By:Brain/0.6b&#39;); //内容语言(en=English) //contentlanguage(en=English) header(&#39;Content-language:en&#39;); //最后修改时间(在缓存的时候可以用到) //lastmodified(goodforcaching) $time=time()-60;//orfilemtime($fn),etc header(&#39;Last-Modified:&#39;.gmdate(&#39;D,dMYH:i:s&#39;,$time).&#39;GMT&#39;); //告诉浏览器要获取的内容还没有更新 //headerfortellingthebrowserthatthecontent //didnotgetchanged header(&#39;HTTP/1.1304NotModified&#39;); //设置内容的长度(缓存的时候可以用到): //setcontentlength(goodforcaching): header(&#39;Content-Length:1234&#39;); //用来下载文件: //Headersforandownload: header(&#39;Content-Type:application/octet-stream&#39;); header(&#39;Content-Disposition:attachment;filename=&quot;example.zip&quot;&#39;); header(&#39;Content-Transfer-Encoding:binary&#39;); //禁止缓存当前文档: //loadthefiletosend:readfile(&#39;example.zip&#39;); //Disablecachingofthecurrentdocument: header(&#39;Cache-Control:no-cache,no-store,max-age=0,must-revalidate&#39;); header(&#39;Expires:Mon,26Jul199705:00:00GMT&#39;); //设置内容类型: //Dateinthepastheader(&#39;Pragma:no-cache&#39;); //setcontenttype: header(&#39;Content-Type:text/html;charset=iso-8859-1&#39;); header(&#39;Content-Type:text/html;charset=utf-8&#39;); header(&#39;Content-Type:text/plain&#39;); //plaintextfile header(&#39;Content-Type:image/jpeg&#39;); //JPGpicture header(&#39;Content-Type:application/zip&#39;); //ZIPfile header(&#39;Content-Type:application/pdf&#39;); //PDFfile header(&#39;Content-Type:audio/mpeg&#39;); //AudioMPEG(MP3,...)file header(&#39;Content-Type:application/x-shockwave-flash&#39;); //显示登录对话框,可以用来进行HTTP认证 //Flashanimation//showsigninbox header(&#39;HTTP/1.1401Unauthorized&#39;); header(&#39;WWW-Authenticate:Basicrealm=&quot;TopSecret&quot;&#39;); print&#39;Textthatwillbedisplayediftheuserhitscancelor&#39;; print&#39;enterswronglogindata&#39;;?&gt;</pre>
RangeTime:0.008427s
RangeMem:211.59 KB
返回顶部 留言