扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

php header函数的用法是什么

扬州沐宇科技
2024-02-27 09:49:57
PHP

PHP中的header()函数用于向客户端发送原始的HTTP头信息。这个函数必须在页面中其他任何输出之前调用,否则会报错。

header()函数通常用于重定向页面、设置cookie、设置缓存控制等功能。例如:

// 重定向到另一个页面
header("Location: http://www.example.com");

// 设置cookie
header("Set-Cookie: username=johndoe");

// 设置缓存控制
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

需要注意的是,header()函数必须在页面输出之前调用,否则会报错。另外,一旦header()函数发送了HTTP头信息,就不能再发送其他输出,否则会导致错误。

扫码添加客服微信