header(string $header, bool $replace = true, int $http_response_code = 0): void
$header:必须,要发送的HTTP头部信息,包括重定向的URL。
$replace:可选,指定是否覆盖之前发送的HTTP头部信息。默认为true,表示覆盖。
$http_response_code:可选,设置HTTP状态码。默认为0,表示不设置状态码。
// 重定向到指定的URL
header('Location: https://www.example.com/');
// 重定向并设置HTTP状态码为301
header('Location: https://www.example.com/', true, 301);
需要注意的是,在调用header()函数之前不能有任何输出,包括HTML标签、空格、换行符等。否则将会导致重定向失败或者出现“Headers already sent”错误。