解析PHP函数header发放HTTP头信息

解析PHP函数header发放HTTP头信息

网上找的,希望对大家有用~~
PHP函数header主要可以帮助我们实现向浏览器等user agent 发放http 头信息的函数。下面我们讲为大家详细介绍有关PHP函数header的相关使用方法。


  • 总结PHP date()参数列表
  • 正确解读PHP获取时间错误原因
  • PHP生成HTML前提条件及原理介绍
  • PHP图表类JpGraph助我们画出炫目图表
  • PHP mail()函数实现发送邮件的方法
    // 向浏览器发送404 状态码
    header(“HTTP/1.1 404 Not Found”);
    或者
    header(“HTTP/1.1 404″);
    // 永久重定向
    header(“Location: http://weizhifeng.net/”);//默认是301 跳转
    // 临时重定向
    header(“HTTP/1.1 302 Found”);
    header(“Location: http://weizhifeng.net/”);
    // 下载文件
    header(“Content-type: text/plain’); // 可以替换成你需要的MIME类型
    header(‘Content-Disposition: attachment; filename=”weizhifeng.txt”‘);
    readfile(‘weizhifeng.txt’);
    其他的头信息可以参考HTTP/1.1 specification
    如果在header之前已经有了输出,那么请使用ob_start()函数。
  • 1 <?php
    2
    3 // ok
    4 header('HTTP/1.1 200 OK');
    5
    6 //设置一个404头:
    7 header('HTTP/1.1 404 Not Found');
    8
    9 //设置地址被永久的重定向
    10 header('HTTP/1.1 301 Moved Permanently');
    11
    12 //转到一个新地址
    13 header('Location: http://www.example.org/');
    14
    15 //文件延迟转向:
    16 header('Refresh: 10; url=http://www.example.org/');
    17 print 'You will be redirected in 10 seconds';
    18
    19 //当然,也可以使用html语法实现
    20 // <meta http-equiv="refresh" content="10;http://www.example.org/ />
    21
    22 // override X-Powered-By: PHP:
    23 header('X-Powered-By: PHP/4.4.0');
    24 header('X-Powered-By: Brain/0.6b');
    25
    26 //文档语言
    27 header('Content-language: en');
    28
    29 //告诉浏览器最后一次修改时间
    30 $time = time() - 60; // or filemtime($fn), etc
    31 header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
    32
    33 //告诉浏览器文档内容没有发生改变
    34 header('HTTP/1.1 304 Not Modified');
    35
    36 //设置内容长度
    37 header('Content-Length: 1234');
    38
    39 //设置为一个下载类型
    40 header('Content-Type: application/octet-stream');
    41 header('Content-Disposition: attachment; filename="example.zip"');
    42 header('Content-Transfer-Encoding: binary');
    43 // load the file to send:
    44 readfile('example.zip');
    45
    46 // 对当前文档禁用缓存
    47 header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
    48 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    49 header('Pragma: no-cache');
    50
    51 //设置内容类型:
    52 header('Content-Type: text/html; charset=iso-8859-1');
    53 header('Content-Type: text/html; charset=utf-8');
    54 header('Content-Type: text/plain'); //纯文本格式
    55 header('Content-Type: image/jpeg'); //JPG图片
    56 header('Content-Type: application/zip'); // ZIP文件
    57 header('Content-Type: application/pdf'); // PDF文件
    58 header('Content-Type: audio/mpeg'); // 音频文件
    59 header('Content-Type: application/x-shockwave-flash'); //Flash动画
    60
    61 //显示登陆对话框
    62 header('HTTP/1.1 401 Unauthorized');
    63 header('WWW-Authenticate: Basic realm="Top Secret"');
    64 print 'Text that will be displayed if the user hits cancel or ';
    65 print 'enters wrong login data';
    66 ?>
    67

     

posted on 2010-12-04 15:14  startup  阅读(1317)  评论(1)    收藏  举报

导航