落后就要挨打,爱拼才会赢。有限的生命也可以创造奇迹,只要每天都有进步!
有一种拼博叫坚持...

首页>>PHP>>PHP之header函数

PHP之header函数

HTTP协议相关总结 一文提示http协议中的header头部信息,下面就结合实例说说header属性在PHP中应用。

PHP中用下面这个函数控制header信息:
void header ( string string [, bool replace [, int http_response_code]] )

header()是用来发送 HTTP Header的。replace是个可选的参数,指示是否替代一个先期相似的header,或者增加第2个相同类型的header。默认的是替代,但是如果你传递FALSE参数给第2个参数(即replace),可以强制使用多个相同类型的header。如:

<?php
header(’WWW-Authenticate: Negotiate’);
header(’WWW-Authenticate: NTLM’, false);
?>          

第二个可选的参数http_response_code强置HTTP响应代码的值。(PHP>4.3.0)

有两种特定的header调用。一种是header用字符串”HTTP/”开头的(大小写无所谓),它用
来指定发出的HTTP状态码。比如,访问一个实际不存在的文件,你可能想确认你的脚本生成一个合适的状态码:

<?php
header(”HTTP/1.0 404 Not Found”);
?>

注意:HTTP状态行 总是最先发送给客户端的,不管你实际上在什么时候调用header()函数。
除非你的HTTP header已经发送出去了,否则,在任何时候,状态将会被header()函数用一个新的
状态行 覆盖。

第二中特殊的情况就是”Location:”header。如果没有某些 3xx的状态码已经发出的话,它不仅
发出Location行,而且发出一个重定向的状态码(302)。

<?php
header(”Location: http://www.thisuc.com”);
exit;
?>

注意:HTTP/1.1要求一个绝对 URI作为Location的参数:包括协议头,主机名和绝对路径,
但是某些客户端可以接受相对URI。你可 用$_SERVER[‘HTTP_HOST’] $_SERVER[‘PHP_SELF’]和
dirname()来从一个相对URI来产生绝对 URI。

<?php
header(”Location: http://” . $_SERVER[‘HTTP_HOST’]
. dirname($_SERVER[‘PHP_SELF’])
. “/” . $relative_url);
?>
PHP脚本通常生成一些动态的内容,不必被客户端的浏览器或者代理服务器缓 存。许多代理和客户端可以强制禁止缓存:

<?php
// 时间总是过去的
header(”Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
// 文件总是被修改的
header(”Last-Modified: ” . gmdate(”D, d M Y H:i:s”) . ” GMT”);

// HTTP/1.1
header(”Cache-Control: no-store, no-cache, must-revalidate”);
header(”Cache-Control: post-check=0, pre-check=0″, false);

// HTTP/1.0
header(”Pragma: no-cache”);
?>

如果你想提示用户保存你 发送过去的数据,比如一个已经生成的PDF文件,你可以用Content_Dispostion头来提供一个推荐的文件名,强迫浏览器显示保存对 话框.

<?php
//保存为pdf文档
header(’Content-type: application/pdf’);
header(’Content-Disposition: attachment; filename=”downloaded.pdf”‘);
readfile(’original.pdf’);

//生成快捷方式:
$Shortcut = “[InternetShortcut]
URL=http://net.thisuc.com //也可以换成动态代码:URL =  “http://”.$_SERVER [‘HTTP_HOST’].$_SERVER[‘PHP_SELF’];
IDList=
IconFile=http://www.thisuc.com/favicon.ico
IconIndex=1
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
“;
Header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=薄日导航.url;”);
echo $Shortcut;

//导出.csv文档:
header(“Content-Disposition: attachment; filename=”test.csv”);
header(‘Content-Type: application/octet-stream’);
?>

对于IE,要加上Connection头
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: “.$url);
header(”Connection: close”);

一些常用的实例:
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”);//ie6 会出现bug Cache-Control: no-cache 后面跟no-cache jsonp 不能执行,建议改为下面的
header(“Cache-Control: max-age=0, must-revalidate”);
header(“Content-Type: application/x-javascript; charset=gbk”);//输出js代码,一般将以上4种组合,用于jsonp调用。

header(“Pragma: no-cache”);
header(“Content-type: text/html;charset=GBK”);//输出编码,避免中文乱码
header(‘Content-type: application/atom+xml’); //Atom
header(‘Content-type: text/css’);
header(‘Content-type: text/javascript’);//Javascript
header(‘Content-type: image/jpeg’);//JPEG Image
header(‘Content-type: application/json’);//JSON
header(‘Content-type: application/pdf’);//PDF
header(‘Content-Type: application/rss+xml; charset=ISO-8859-1’);//RSS
header(‘Content-type: text/plain’);//纯文本格式
header(‘Content-type: text/xml’);//XML
header(‘Content-Type: audio/mpeg’); // 音频文件
header(‘Content-Type: application/x-shockw**e-flash’); //Flash动画
header(‘HTTP/1.1 200 OK’);// ok
header(‘HTTP/1.1 404 Not Found’);设置一个404头
header(‘HTTP/1.1 301 Moved Permanently’);//设置地址被永久的重定向
header(‘Location: http://www.example.org/’);//转到一个新地址
header(‘Refresh: 10; url=http://www.example.org/’);//文件延迟转向:
<meta http-equiv=”refresh” content=”10;http://www.example.org/ /> //当然,也可以使用html语法实现
// override X-Powered-By: PHP:
header(‘X-Powered-By: PHP/4.4.0’);
header(‘X-Powered-By: Brain/0.6b’);

header(‘Content-language: en’);//文档语言
header(‘HTTP/1.1 304 Not Modified’);//告诉浏览器文档内容没有发生改变
header(‘Content-Length: 1234’);//设置内容长度

//设置为一个下载类型
header(‘Content-Type: application/octet-stream’);
header(‘Content-Disposition: attachment; filename=”example.zip”‘);
header(‘Content-Transfer-Encoding: binary’);
//load the file to send:
readfile(‘example.zip’);

//显示登陆对话框
header(‘HTTP/1.1 401 Unauthorized’);
header(‘WWW-Authenticate: Basic realm=”Top Secret”‘);

header 中Content-type的种类:
‘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’

 

« 上一篇: 下一篇:»

相关文章:



发表评论

You must be logged in to post a comment.