搜索
首页后端开发php教程详细解释PHP中header和content-type 参数_PHP教程

void header ( string string [, bool replace [, int http_response_code]] )
void header ( string string [, bool replace [, int http_response_code]] )

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

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

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

有两种特定的 header调 用。一种是header用字符串”HTTP/”开头的(大小写无所谓),它用
来指定发出的HTTP状态码。举个例子来说,如果你已经配置 Apache使用javascript:;" onClick="javascript:tagshow(event, 'PHP');" target="_self">PHP脚 本来处理请求
不存在的文件(用ErrorDocument指令),你可能想确认你的脚本生成一个合适的状态码:

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

注意:HTTP状态行 总是最先发送给客户端的,不管你实际上在什么时候调用header()函数。
除非你的HTTP header已经发送出去了,否则,在任何时候,状态将会被header()函数用一个新的
状态行 覆盖。
在PHP 3中,只有PHP编译为Apache的模块是,并且用下面Staus Header才能取得同样的效果

header(”Status: 404 Not Found”);
?>

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

header(”Location: http://www.BkJia.com”);
exit;
?>

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

header(”Location: http://” . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. “/” . $relative_url);
?>

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”);
?>

注意:你可能发现即使你没有输出上面的header,你的页面还是不能被 缓存。可能是有几个选项改变了默认的
缓存行为。见sessrion_cache_limiter().

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

header(’Content-type: application/pdf’);
header(’Content-Disposition: attachment; filename=”downloaded.pdf”‘);
readfile(’original.pdf’);
?>

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

header 中Content-type的种类 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-javas
cript',
'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'

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478684.htmlTechArticlevoid header ( string string [, bool replace [, int http_response_code]] ) void header ( string string [, bool replace [, int http_response_code]] ) header()是用来发送 HTTP Hea...
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何在PHP中创建和使用接口?您如何在PHP中创建和使用接口?Apr 30, 2025 pm 03:40 PM

本文解释了如何创建,实施和使用PHP中的接口,重点关注其对代码组织和可维护性的好处。

crypt()和password_hash()有什么区别?crypt()和password_hash()有什么区别?Apr 30, 2025 pm 03:39 PM

本文讨论了PHP中的crypt()和password_hash()之间的差异,以进行密码哈希,重点介绍其实施,安全性和对现代Web应用程序的适用性。

如何防止PHP中的跨站点脚本(XSS)?如何防止PHP中的跨站点脚本(XSS)?Apr 30, 2025 pm 03:38 PM

文章讨论了通过输入验证,输出编码以及使用OWASP ESAPI和HTML净化器之类的工具来防止PHP中的跨站点脚本(XSS)。

PHP中的自动加载是什么?PHP中的自动加载是什么?Apr 30, 2025 pm 03:37 PM

自动加载PHP会在需要时自动加载类文件,从而通过减少内存使用和增强代码组织来提高性能。最佳实践包括使用PSR-4和有效组织代码。

什么是PHP流?什么是PHP流?Apr 30, 2025 pm 03:36 PM

PHP流通过一致的API统一处理文件,网络插座和压缩格式等资源的处理,从而提高复杂性并增强代码灵活性和效率。

可以使用PHP上传的文件的最大大小是多少?可以使用PHP上传的文件的最大大小是多少?Apr 30, 2025 pm 03:35 PM

本文讨论了在PHP中管理文件上传大小的管理,重点是2MB的默认限制以及如何通过修改PHP.INI设置来增加它。

PHP中的无效类型是什么?PHP中的无效类型是什么?Apr 30, 2025 pm 03:34 PM

本文讨论了PHP 7.1中引入的PHP中的无效类型,允许变量或参数为指定类型或NULL。它突出显示了诸如提高可读性,类型安全性和明确意图的好处,并解释了如何声明

unset()和unlink()函数之间有什么区别?unset()和unlink()函数之间有什么区别?Apr 30, 2025 pm 03:33 PM

本文讨论了unset()和unlink()功能在编程中的差异,重点关注其目的和用例。 unset()从内存中删除变量,而unlink()从文件系统中删除文件。两者都对效率至关重要

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具