💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### PHP 中 header 用法详解 header is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers. 参考:http://php.net/manual/en/function.header.php 范例一: ~~~ <!--?PHP Header("Location: http://www.jb51.net";); exit;//在每个重定向之后都必须加上“exit",避免发生错误后,继续执行。 ?--> ~~~ ~~~ <!--?php header("refresh:2;url=http://www.jb51.net"); echo "正在加载,请稍等...<br-->三秒后自动跳转至百度..."; ?--> ~~~ 范例三: 让使用者的浏览器出现找不到档案的信息,正确的写法是: ~~~ header("http/1.1 404 Not Found"); ~~~ 第一部分为HTTP协议的版本(`HTTP-Version`);第二部分为状态代码(`Status`);第三部分为原因短语(`Reason-Phrase`)。 范例四: 让使用者下载档案( 隐藏文件的位置 ) html标签 就可以实现普通文件下载。如果为了保密文件,就不能把文件链接告诉别人,可以用header函数实现文件下载。 ~~~ <!--?php header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=文件名/"); header("Content-Description: PHP3 Generated Data"); ?--> ~~~ 范例五: header函数前不能够输出内容 一般来说在header函数前不能输出html内容,就是说在这些函数的前面不能有任何文字、空行、回车等,而且最好在header()函数后加上exit()函数。 类似的还有 `setcookie()` 和 `session` 函数,这些函数需要在输出流中增加消息头部信息。如果在`header()`执行之前有`echo`等语句,当后面遇到`header()`时,就会报出 “Warning: Cannot modify header information - headers already sent by ….”错误。 例如,下面的正确写法: ~~~ <!--?php header("http/1.1 403 Forbidden"); exit(); ?--> ~~~ * * * * * ~~~ <!--?php // ok header('HTTP/1.1 200 OK'); //设置一个404头: header('HTTP/1.1 404 Not Found'); //设置地址被永久的重定向 header('HTTP/1.1 301 Moved Permanently'); //转到一个新地址 header('Location: http://www.baidu.com'); //文件延迟转向: header('Refresh: 10; url=http://www.example.org/'); print 'You will be redirected in 10 seconds'; //当然,也可以使用html语法实现 // <cke:encoded-->%3Cmeta%20http-equiv%3D%22refresh%22%20content%3D%2210%3Bhttp%3A%2F%2Fwww.example.org%2F%20%2F%3E // override X-Powered-By: PHP: header('X-Powered-By: PHP/4.4.0'); header('X-Powered-By: Brain/0.6b'); //文档语言 header('Content-language: en'); //告诉浏览器最后一次修改时间 $time = time() - 60; // or filemtime($fn), etc header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告诉浏览器文档内容没有发生改变 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('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Pragma: no-cache'); //设置内容类型: header('Content-Type: text/html; charset=iso-8859-1'); header('Content-Type: text/html; charset=utf-8'); header('Content-Type: text/plain'); //纯文本格式 header('Content-Type: image/jpeg'); //JPG图片 header('Content-Type: application/zip'); // ZIP文件 header('Content-Type: application/pdf'); // PDF文件 header('Content-Type: audio/mpeg'); // 音频文件 header('Content-Type: application/x-shockwave-flash'); //Flash动画 //显示登陆对话框 header('HTTP/1.1 401 Unauthorized'); header('WWW-Authenticate: Basic realm="Top Secret"'); print 'Text that will be displayed if the user hits cancel or '; print 'enters wrong login data'; ?--> ~~~