php中的header()函数

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');

}
?> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值