1 重定向功能,这种最常见
<?php
Header("Location: http://www.php.net");
//注意:Location:后面有一空格.
?>
2 强制用户每次访问这个页面时获取最新资料,而不是使用存在客户端的缓存。
<?php
//告诉浏览器此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可。
header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
//告诉浏览器此页面的最后更新日期(用格林威治时间表示)也就是当天,目的就是强迫浏览器获取最新资料
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
//告诉客户端浏览器不使用缓存
header("Cache-Control: no-cache, must-revalidate");
//参数(与以前的服务器兼容),即兼容HTTP1.0协议
header("Pragma: no-cache");
//输出MIME类型
header("Content-type: application/file");
//文件长度
header("Content-Length: 227685");
//接受的范围单位
header("Accept-Ranges: bytes");
//缺省时文件保存对话框中的文件名称
header("Content-Disposition: attachment; filename=$filename");
?>
3 输出状态值到浏览器,主要用于访问权限控制
<?php
header('HTTP/1.1 401 Unauthorized');
header('status: 401 Unauthorized');
?>
比如要限制一个用户不能访问该页,则可设置状态为404,如下所示,这样浏览器就显示为即该页不存在
<?php
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>
//较为完整的download函数,用于在浏览器上下载
<?php
function downloadFile($fullPath ){
//如果已发送了头信息,则退出
if( headers_sent() )
die('Headers Sent');
//关闭页面缓存
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression','Off');
// 检查文件是是否存在
if( file_exists($fullPath) ){
// 获得文件的扩展名
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// 决定内容的类型
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullPath);
} else
die('File Not Found');
}
?>