PHP中的header函数用于发送HTTP头信息,可以通过设置不同的参数来实现不同的功能。
- 使用header函数设置HTTP响应状态码:
header("HTTP/1.1 200 OK");
- 使用header函数设置响应内容类型:
header("Content-Type: text/html; charset=utf-8");
- 使用header函数设置重定向:
header("Location: http://www.example.com");
- 使用header函数设置缓存控制:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
注意:在使用header函数之前不能有任何输出,否则会导致header无法正常工作。