Home >Backend Development >PHP Tutorial >Detailed explanation of php header function_PHP tutorial
网页的缓存是由 HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、<span max</span>-age、must-<span revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况: (</span>1<span ) 打开新窗口 值为private、no</span>-cache、must-<span revalidate,那么打开新窗口访问时都会重新访问服务器。 而如果指定了max</span>-<span age值,那么在此值内的时间里就不会重新访问服务器,例如: Cache</span>-control: <span max</span>-age=5<span (表示当访问此网页后的5秒内再次访问不会去服务器) (</span>2<span ) 在地址栏回车 值为private或must</span>-<span revalidate则只有第一次访问时会访问服务器,以后就不再访问。 值为no</span>-<span cache,那么每次都会访问。 值为max</span>-<span age,则在过期之前不会重复访问。 (</span>3<span ) 按后退按扭 值为private、must</span>-revalidate、<span max</span>-<span age,则不会重访问, 值为no</span>-<span cache,则每次都重复访问 (</span>4<span ) 按刷新按扭 无论为何值,都会重复访问 Cache</span>-control值为“no-<span cache”时,访问此页面不会在Internet临时文章夹留下页面备份。 另外,通过指定“Expires”值也会影响到缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问: Expires</span>: Fri, 31 Dec 1999 16:00:00<span GMT 比如:禁止页面在IE中缓存 HTTP响应消息头部设置: CacheControl </span>= no-<span cache Pragma</span>=no-<span cache Expires </span>= -1<span Expires是个好东东,如果服务器上的网页经常变化,就把它设置为</span>-1<span ,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。 当HTTP1</span>.1服务器指定 CacheControl = no-<span cache时,浏览器就不会缓存该网页。 旧式 HTTP </span>1.0 服务器不能使用 Cache-<span Control 标题。 所以为了向后兼容 HTTP </span>1.0 服务器,IE使用Pragma:no-<span cache 标题对 HTTP 提供特殊支持。 如果客户端通过安全连接 (https</span>:<span //</span><span )/ 与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,</span> 则 Internet Explorer不会缓存此响应。注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-<span 1相同,该页将被缓存,但被标记为立即过期 header常用指令 header分为三部分: 第一部分为HTTP协议的版本(HTTP</span>-<span Version); 第二部分为状态代码(Status); 第三部分为原因短语(Reason</span>-<span Phrase)。 </span><span //</span><span 向浏览器发送404 状态码</span> <span header</span>(“HTTP/1.1 404<span Not Found”); 或者 </span><span header</span>(“HTTP/1.1 404<span ″); </span><span //</span><span 永久重定向</span> <span header</span>(“Location: http:<span //</span><span hi.baidu.com/3800_hk”);//默认是301 跳转 // 临时重定向</span> <span header</span>(“HTTP/1.1 302<span Found”); </span><span header</span>(“Location: http:<span //</span><span hi.baidu.com/3800_hk”); // 下载文件</span> <span header</span>(“Content-type: text/plain’); <span //</span><span 可以替换成你需要的MIME类型</span> <span header</span>(‘Content-Disposition: attachment; filename=”weizhifeng.<span txt”‘); </span><span readfile</span>(‘weizhifeng.<span txt’); 其他的头信息可以参考HTTP</span>/1.1<span specification 如果在header之前已经有了输出,那么请使用ob_start()函数。 </span><?<span php </span><span header</span>('HTTP/1.1 200 OK'<span ); </span><span //</span><span 设置一个404头:</span> <span header</span>('HTTP/1.1 404 Not Found'<span ); </span><span //</span><span 设置地址被永久的重定向</span> <span header</span>('HTTP/1.1 301 Moved Permanently');<span //</span><span 转到一个新地址</span> <span header</span>('Location: http://www.example.org/'<span ); </span><span //</span><span 文件延迟转向:</span> <span header</span>('Refresh: 10; url=http://www.example.org/'<span ); </span><span print</span>'You will be redirected in 10 seconds'<span ; </span><span //</span><span 当然,也可以使用html语法实现 // <meta http-equiv="refresh" content="10;http://www.example.org/ /> // override X-Powered-By: PHP:</span> <span header</span>('X-Powered-By: PHP/4.4.0'<span ); </span><span header</span>('X-Powered-By: Brain/0.6b'<span ); </span><span //</span><span 文档语言</span> <span header</span>('Content-language: en'<span ); </span><span //</span><span 告诉浏览器最后一次修改时间</span> <span $time</span>=<span time</span>() -60; <span //</span><span or filemtime($fn), etc</span> <span header</span>('Last-Modified: '.<span gmdate</span>('D, d M Y H:i:s',<span $time</span>).' GMT'<span ); </span><span //</span><span 告诉浏览器文档内容没有发生改变</span> <span header</span>('HTTP/1.1 304 Not Modified');<span //</span><span 设置内容长度</span> <span header</span>('Content-Length: 1234'<span ); </span><span //</span><span 设置为一个下载类型</span> <span header</span>('Content-Type: application/octet-stream'<span ); </span><span header</span>('Content-Disposition: attachment; filename="example.zip"'<span ); </span><span header</span>('Content-Transfer-Encoding: binary'<span ); </span><span //</span><span load the file to send:</span> <span readfile</span>('example.zip'<span ); </span><span //</span><span 对当前文档禁用缓存</span> <span header</span>('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'<span ); </span><span header</span>('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); <span //</span><span Date in the past</span> <span header</span>('Pragma: no-cache'<span ); </span><span //</span><span 设置内容类型:</span> <span header</span>('Content-Type: text/html; charset=iso-8859-1'<span ); </span><span header</span>('Content-Type: text/html; charset=utf-8'<span ); </span><span header</span>('Content-Type: text/plain'); <span //</span><span 纯文本格式</span> <span header</span>('Content-Type: image/jpeg'); <span //</span><span JPG图片</span> <span header</span>('Content-Type: application/zip'); <span //</span><span ZIP文件</span> <span header</span>('Content-Type: application/pdf'); <span //</span><span PDF文件</span> <span header</span>('Content-Type: audio/mpeg'); <span //</span><span 音频文件</span> <span header</span>('Content-Type: application/x-shockwave-flash'); <span //</span><span Flash动画 //显示登陆对话框</span> <span header</span>('HTTP/1.1 401 Unauthorized'<span ); </span><span header</span>('WWW-Authenticate: Basic realm="Top Secret"'<span ); </span><span print</span>'Text that will be displayed if the user hits cancel or '<span ; </span><span print</span>'enters wrong login data'<span ; </span>?>