龙空技术网

PHP常用header头大全

程序员青叶 337

前言:

此刻咱们对“nginxheader跳转”大概比较关怀,小伙伴们都想要了解一些“nginxheader跳转”的相关资讯。那么小编在网络上汇集了一些关于“nginxheader跳转””的相关内容,希望咱们能喜欢,各位老铁们一起来了解一下吧!

前言

在日常的php的开发中,我们常常需要使用到header函数头来进行做标记。

这里好奇心就带大家一起列出我们常用的header头,供大家参考。

返回HTTP状态码

如果你对http状态码比较感兴趣,可以参考我之前的文章:

HTTP状态码超详细说明

常见HTTP状态码汇总说明

200 正常访问

header('HTTP/1.1 200 OK'); 

301 设置地址被永久的重定向,进行相应跳转

header('HTTP/1.1 301 Moved Permanently');

304 告诉浏览器文档内容没有发生改变

header('HTTP/1.1 304 Not Modified'); 

403 设置当前页面禁止访问

header('HTTP/1.1 403 Forbidden');

404 通知浏览器 页面不存在

header('HTTP/1.1 404 Not Found'); 

500 服务器错误

header('HTTP/1.1 500 Internal Server Error');
网页重定向

跳转到一个新的地址,如头条的网站

header('Location: ;); 

延迟转向 也就是隔几秒跳转,也常用于页面刷新

header('Refresh: 10; url=;); 
修改头信息

修改 X-Powered-By信息(这个一般是Apache/Nginx等自己相应返回的

header('X-Powered-By: PHP/8.0.0'); 

指定文档语言

header('Content-language: en'); 

设置内容长度

header('Content-Length: 5566'); 

通知浏览器最后一次修改时间

header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); 

设置此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可。

 header("Expires: Mon, 12 Jul 2020 01:03:04 GMT");      

设置此页面的最后更新日期(用格林威治时间表示)为当天,可以强制浏览器获取最新内容

 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");      

告诉客户端浏览器不使用缓存,适用于HTTP 1.1 协议

header("Cache-Control: no-cache, must-revalidate");      

告诉客户端浏览器不使用缓存,兼容HTTP 1.0 协议

header("Pragma: no-cache");    

HTTP缓存我之前也有详细讲过,可以参考:一文了解HTTP缓存

内容类型

设置网页编码

header('Content-Type: text/html; charset=utf-8'); 

设置纯文本格式

header('Content-Type: text/plain'); 

其他常见内容类型

 // jpg jpeg文件header('Content-Type: image/jpeg');// zip压缩文件header('Content-Type: application/zip');  // PDF文件header('Content-Type: application/pdf'); // 音频文件header('Content-Type: audio/mpeg'); //css文件header('Content-type: text/css'); //js文件header('Content-type: text/javascript'); //jsonheader('Content-type: application/json'); //pdfheader('Content-type: application/pdf'); //xml格式文件header('Content-type: text/xml'); //Flash动画header('Content-Type: application/x-shockw**e-flash'); 
声明一个下载的文件
header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename="haoqixin.zip"'); // 这里以haoqixin.zip为演示header('Content-Transfer-Encoding: binary');readfile('haoqixin.zip');
对当前文档禁用缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');header('Expires: Mon, 16 Jul 2021 09:30:00 GMT');
显示一个需要验证的登陆对话框
header('HTTP/1.1 401 Unauthorized');header('WWW-Authenticate: Basic realm="Top Secret"');
声明一个需要下载的xls文件
header('Content-Disposition: attachment; filename=toutiao.xlsx');header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('Content-Length: '.filesize('./haoqixin.xls'));header('Content-Transfer-Encoding: binary');header('Cache-Control: must-revalidate');header('Pragma: public');readfile('./haoqixin.xls');
总结

好了,在PHP中我们常用的Header头基本就这些了,希望对你有帮助。

标签: #nginxheader跳转