PHP 入门:php 中 header 的用法说明
本文主要为大家介绍 php 中 header 的用法进行了详细说明,供大家参考下
header() is used to send raw HTTP headers. See the HTTP/1.1 specification
for
more information
on HTTP headers.
范例一:
代码如下:
<?PHP
Header("Location: ";);
exit
;
//
“
在每个重定向之后都必须加上 exit",避免发生错误后,继续执行。
?>
代码如下:
<?php
header("refresh:2;url=");
echo
"正在加载,请稍等...<br>三秒后自动跳转至<a href="
" mce_href="">百度</a>...";
?>
--------------------------------------------------------------------------------
范例二:禁止页面在 IE
中缓存
使浏览者每次都能得到最新的资料,而不是 Proxy
或 cache 中的资料:
代码如下:
<?PHP
header( 'Expires: Fri, 4 Dec 2009 09:00:00 GMT' );
header( 'Last-Modified: ' .
gmdate
( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
//兼容 http1.0 和 https
?>
CacheControl = no-cache Pragma=no-cache Expires = -1
如果服务器上的网页经常变化,就把 Expires 设置为-1,表示立即过期。如果一个网页每
天凌晨 1 点更新,可以把 Expires 设置为第二天的凌晨 1 点。当 HTTP1.1 服务器指定
CacheControl = no-cache 时,浏览器就不会缓存该网页。
旧式 HTTP 1.0
服务器不能使用 Cache-Control
标题。所以为了向后兼容 HTTP 1.0 服务器,
IE 使用 Pragma:no-cache
标题对 HTTP
提供特殊支持。如果客户端通过安全连接 (https:
//)
与服务器通讯,且服务器在响应中返回 Pragma:no-cache
标题,则 Internet Explorer 不会缓
存此响应。