PHP中header()函数的七种用法小结

 更新时间:2023年08月29日 11:47:59   作者:L小臣  
我们在实际开发中经常使用header()实现一些功能,这篇文章介绍关于header()的7中用法,文中有详细的代码示例,具有一定的参考价值,需要的朋友可以参考下

PHP header()的7中用法:

1、跳转页面

可以使用header()实现跳转页面功能。

header('Location:'.$url); // $url 跳转页面的地址

2、声明 content-type

调用API接口时,一般都会声明 content-type 的类型,否则无法读写数据。

header('content-type:text/html;charset=utf-8');
header('content-type:application/json;charset=utf-8');

3、返回response状态码

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

4、定时执行跳转

header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳转。在这里插入代码片

5、控制浏览器缓存

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

6、执行http验证

header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');

7、下载

header('Content-Type: application/octet-stream'); //设置内容类型
header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件
header('Content-Transfer-Encoding: binary'); //设置传输方式
header('Content-Length: '.filesize('example.zip')); //设置内容长度

到此这篇关于PHP中header()的七种用法小结的文章就介绍到这了,更多相关PHP header()用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论