在PHP中,可以使用header()
函数来设置HTTP头。该函数用于发送原始的HTTP头信息。
下面是一些常见的用法:
设置Content-Type头:
header('Content-Type: text/html');
设置重定向头:
header('Location: http://www.example.com');
设置缓存控制头:
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
设置下载文件头:
header('Content-Disposition: attachment; filename="example.txt"');
请注意,header()
函数必须在任何实际输出之前调用,包括HTML标记、空格和换行符。否则,将会收到"headers already sent"的错误。
如果要发送多个头信息,可以多次调用header()
函数。
另外,为了避免一些安全问题,建议在设置Location
头之后,使用exit
或die
函数终止脚本的执行,以防止继续执行后面的代码。
上一篇:php正则如何删除符号
下一篇:如何开启php错误提示
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站