PHP中HEADER头消息详解

发布时间:2019-09-22编辑:脚本学堂
PHP中经常需要通过HEADER发送HTTP标头消息,以便告诉浏览器一些处理状态(Status)或显示参量,最典型的就是发送页面处理HEADER和发送状态HEADER。

无论是发送哪种HEADER消息,都必须保证在发送HEADER之前没有输出任何信息到终端浏览器。

一、使用HEADER发送文本类型头信息
例1:
 

复制代码 代码如下:
<?php
header("Content-Type: text/html; charset=UTF-8");//告知各位观众下面将要输出的文本类型
?>


例1输出HEADER告诉浏览器将要输出的文本编码格式为UTF-8。这在全页面PHP处理上是必须的,否则可能会引起终端浏览器显示乱码或空白页显示现象。由于PHP指定在HEADER输出前不能有任何输出信息,包括空格,所以当页面运行出现HEADER信息已经发送的提示时候,需要首先检查在HEADER之前是否输出了某些信息,包括本PHP页面的包含头文件是否输出信息。
如果需要输出文件提示下载可使用如下方法:
 

复制代码 代码如下:
<?php
header("Content-type: application/octet-stream");//FILE流
header("Accept-Ranges: bytes");
header("Accept-Length: $filesize");//提示将要接收的文件大小
header("Content-Disposition: attachment; filename=".$fname); //提示终端浏览器下载操作
?>
 

如果是PDF格式也可用header("Content-type: application/pdf");其它格式类似处理,不过上面的例子是个下载文件流通例,但是指定详细的文本格式似乎对浏览器的默认处理似乎更好,比如让播放器自动播放下载音乐、或PDF编辑器自动打开远程PDF文档等。

附Content-type的MIME类型说明:
 

复制代码 代码如下:
<?php
$mimetypes = array(
        'ez' => 'application/andrew-inset',
        'hqx' => 'application/mac-binhex40',
        'cpt' => 'application/mac-compactpro',
        'doc' => 'application/msword',
        'bin' => 'application/octet-stream',
        'dms' => 'application/octet-stream',
        'lha' => 'application/octet-stream',
        'lzh' => 'application/octet-stream',
        'exe' => 'application/octet-stream',
        'class' => 'application/octet-stream',
        'so' => 'application/octet-stream',
        'dll' => 'application/octet-stream',
        'oda' => 'application/oda',
        'pdf' => 'application/pdf',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',
        'smi' => 'application/smil',
        'smil' => 'application/smil',
        'mif' => 'application/vnd.mif',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'wbxml' => 'application/vnd.wap.wbxml',
        'wmlc' => 'application/vnd.wap.wmlc',
        'wmlsc' => 'application/vnd.wap.wmlscriptc',
        'bcpio' => 'application/x-bcpio',
        'vcd' => 'application/x-cdlink',
        'pgn' => 'application/x-chess-pgn',
        'cpio' => 'application/x-cpio',
        'csh' => 'application/x-csh',
        'dcr' => 'application/x-director',
        'dir' => 'application/x-director',
        'dxr' => 'application/x-director',
        'dvi' => 'application/x-dvi',
        'spl' => 'application/x-futuresplash',
        'gtar' => 'application/x-gtar',
        'hdf' => 'application/x-hdf',
        'js' => 'application/x-javascript',
        'skp' => 'application/x-koan',
        'skd' => 'application/x-koan',
        'skt' => 'application/x-koan',
        'skm' => 'application/x-koan',
        'latex' => 'application/x-latex',
        'nc' => 'application/x-netcdf',
        'cdf' => 'application/x-netcdf',
        'sh' => 'application/x-sh',
        'shar' => 'application/x-shar',
        'swf' => 'application/x-shockwave-flash',
        'sit' => 'application/x-stuffit',
        'sv4cpio' => 'application/x-sv4cpio',
        'sv4crc' => 'application/x-sv4crc',
        'tar' => 'application/x-tar',
        'tcl' => 'application/x-tcl',
        'tex' => 'application/x-tex',
        'texinfo' => 'application/x-texinfo',
        'texi' => 'application/x-texinfo',
        't' => 'application/x-troff',
        'tr' => 'application/x-troff',
        'roff' => 'application/x-troff',
        'man' => 'application/x-troff-man',
        'me' => 'application/x-troff-me',
        'ms' => 'application/x-troff-ms',
        'ustar' => 'application/x-ustar',
        'src' => 'application/x-wais-source',
        'xhtml' => 'application/xhtml+xml',
        'xht' => 'application/xhtml+xml',
        'zip' => 'application/zip',
        'au' => 'audio/basic',
        'snd' => 'audio/basic',
        'mid' => 'audio/midi',
        'midi' => 'audio/midi',
        'kar' => 'audio/midi',
        'mpga' => 'audio/mpeg',
        'mp2′ => 'audio/mpeg',
        'mp3′ => 'audio/mpeg',
        'aif' => 'audio/x-aiff',
        'aiff' => 'audio/x-aiff',
        'aifc' => 'audio/x-aiff',
        'm3u' => 'audio/x-mpegurl',
        'ram' => 'audio/x-pn-realaudio',
        'rm' => 'audio/x-pn-realaudio',
        'rpm' => 'audio/x-pn-realaudio-plugin',
        'ra' => 'audio/x-realaudio',
        'wav' => 'audio/x-wav',
        'pdb' => 'chemical/x-pdb',
        'xyz' => 'chemical/x-xyz',
        'bmp' => 'image/bmp',
        'gif' => 'image/gif',
        'ief' => 'image/ief',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'jpe' => 'image/jpeg',
        'png' => 'image/png',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'djvu' => 'image/vnd.djvu',
        'djv' => 'image/vnd.djvu',
        'wbmp' => 'image/vnd.wap.wbmp',
        'ras' => 'image/x-cmu-raster',
        'pnm' => 'image/x-portable-anymap',
        'pbm' => 'image/x-portable-bitmap',
        'pgm' => 'image/x-portable-graymap',
        'ppm' => 'image/x-portable-pixmap',
        'rgb' => 'image/x-rgb',
        'xbm' => 'image/x-xbitmap',
        'xpm' => 'image/x-xpixmap',
        'xwd' => 'image/x-xwindowdump',
        'igs' => 'model/iges',
        'iges' => 'model/iges',
        'msh' => 'model/mesh',
        'mesh' => 'model/mesh',
        'silo' => 'model/mesh',
        'wrl' => 'model/vrml',
        'vrml' => 'model/vrml',
        'css' => 'text/css',
        'html' => 'text/html',
        'htm' => 'text/html',
        'asc' => 'text/plain',
        'txt' => 'text/plain',
        'rtx' => 'text/richtext',
        'rtf' => 'text/rtf',
        'sgml' => 'text/sgml',
        'sgm' => 'text/sgml',
        'tsv' => 'text/tab-separated-values',
        'wml' => 'text/vnd.wap.wml',
        'wmls' => 'text/vnd.wap.wmlscript',
        'etx' => 'text/x-setext',
        'xsl' => 'text/xml',
        'xml' => 'text/xml',
        'mpeg' => 'video/mpeg',
         'mpg' => 'video/mpeg',
        'mpe' => 'video/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',
        'mxu' => 'video/vnd.mpegurl',
        'avi' => 'video/x-msvideo',
        'movie' => 'video/x-sgi-movie',
        'ice' => 'x-conference/x-cooltalk',
);
?>

二、使用HEADER发送状态(Status)信息
当我们在响应浏览器操作时候,可能需要输出一些提示状态信息,比如访问的文件不存(404状态)在或者文件被转移(301状态)等,告诉用户(包括搜索引擎)所访问页面的当前状态,这在实际使用中对站长非常重要,因为这直接影响到搜索引擎对当前页面的判定。当前对于301固定转移(HTTP/1.1 301 Moved Permanently)我们也可以采用其它方式(.htaccess或IIS重定向),但是了解PHP自编程解决这些页面状态提示会加深自己对浏览器状态的认知。
HEADER状态消息头格式如下:
header("第一部分 第二部分 第三部分");
其中第一部分为HTTP协议的版本号(HTTP-Version),第二部分为将要发送的状态代码(Status),第三部分为状态原因(Reason-Phrase) ,三部分中间用一个空格分割,第一部分和第二部分都是必需,第三部分的状态原因为可选项,一般推荐按照标准写出(参下文)。
例2:
 

复制代码 代码如下:
<?php
header("HTTP/1.1 404 Not Found");
?>


例2实际在使用中很少用到的,感觉有点忽悠终端浏览者的意思。
最经常遇到是下面的例3:
 

复制代码 代码如下:
<?php
header("HTTP/1.1 301 Moved Permanently");//301永久迁移
header("Location: http://www.jb200.com");//跳转新url地址,默认Location可能会发送302头,笔者暂不确定。
?>
 

此语句告诉搜索引擎或浏览者,本页面已经被“蚂蚁搬家”了,走,我带你看看新家去(新网页)。

下面专门把W3上Header状态代码(Status)罗列一下供各位使用参考:
1、状态码类(Status)定义
1xx类:Informational - Request received, continuing process(信息状态码类:请求已收到,继续处理)
2xx类:Success - The action was successfully received, understood,and accepted(成功状态码类:本次操作已收到并处理OK)
3xx类:Redirection - Further action must be taken in order to complete the request (重定向状态码类:完成请求前必须进一步执行操作)
4xx类:Client Error - The request contains bad syntax or cannot be fulfilled(发往客户端的错误状态码类:请求包含语法错误或请求不能完成)
5xx类:Server Error - The server failed to fulfill an apparently valid request (服务器端的错误状态码类:服务器执行合法请求失败)
2、常用状态码(Status)列表(部分):
| "100" ; continue
| "101" ; Switching Protocols

| "200" ; OK
| "201" ; Created
| "202" ; Accepted
| "203" ; Non-Authoritative Information
| "204" ; No Content
| "205" ; Reset Content
| "206" ; Partial Content

| "300" ; Multiple Choices
| "301" ; Moved Permanently
| "302" ; Moved Temporarily
| "303" ; See Other
| "304" ; Not Modified
| "305" ; Use Proxy
| "400" ; Bad Request
| "401" ; Unauthorized
| "402" ; Payment required
| "403" ; Forbidden
| "404" ; Not Found
| "405" ; Method Not Allowed
| "406" ; Not Acceptable
| "407" ; Proxy Authentication Required
| "408" ; Request Time-out
| "409" ; Conflict
| "410" ; Gone
| "411" ; Length Required
| "412" ; Precondition Failed
| "413" ; Request Entity Too Large
| "414" ; Request-URI Too Large
| "415" ; Unsupported Media Type

| "500" ; Internal Server Error
| "501" ; Not Implemented
| "502" ; Bad Gateway
| "503" ; Service Unavailable
| "504" ; Gateway Time-out
| "505" ; HTTP Version not supported

另外我们还可以使用PHP 的HEADER发送一些缓存参数命令、超期命令等,例如:
 

复制代码 代码如下:
<?php
header("Cache-Control: no-cache, must-revalidate"); //不要缓存
header("Expires: Fri, 1 Dec 2099 08:00:00 GMT"); // 页面过期时间
?>

您可能感兴趣的文章:
php header()函数的简单例子
php header函数实现文件下载的实例代码
php中header函数的用法举例详解
php header 使用详解
php header函数 文件下载时直接提示保存的代码
php header头信息应用举例
php 文件头部(header)信息详解
PHP header()函数使用详解
php header函数使用要点