php的header作用,PHP header()用法大全
时间: 2023-11-26 07:02:54 浏览: 103
PHP的header()函数用于向客户端发送HTTP头,比如向浏览器发送HTML页面、CSS样式、JavaScript脚本等。
下面是一些常见的用法:
- 发送HTML页面
header('Content-Type: text/html; charset=utf-8');
- 重定向到其他页面
header('Location: http://www.example.com/');
exit;
- 下载文件
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
readfile('example.zip');
- 缓存控制
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
- 设置Cookie
header('Set-Cookie: name=value; expires=Wed, 29 Jan 2020 15:00:00 GMT; path=/; domain=example.com; secure; HttpOnly');
注意:使用header()函数时,必须在输出任何内容之前调用它。否则,会产生“Cannot modify header information - headers already sent”错误。
相关推荐















