PHP headers_sent() 函数
定义和用法
headers_sent() 函数检查标头是否已发送/发送到何处。
实例
例子 1
如果没有发送标头,则发送一个:
<?php
if (!headers_sent()) {
header("Location: https://www.w3school.com.cn/");
exit;
}
?>
<html>
<body>
...
...
例子 2
使用可选的 file 和 line 参数:
<?php
// 传入 $file 和 $line 以供后续使用
// 不要预先给它们赋值
if (!headers_sent($file, $line))
{
header("Location: https://www.w3school.com.cn/");
exit;
// 此处触发错误
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
语法
headers_sent(file,line)
参数值
| 参数 | 描述 |
|---|---|
| file | 可选。如果设置了 file 和 line 参数,headers_sent() 将在 file 和 line 变量中放置 PHP 源文件名和输出开始的行号。 |
| line | 可选。指定输出开始的行号。 |
技术细节
| 返回值: | 如果已发送 HTTP 头部,则返回 TRUE,否则返回 FALSE。 |
|---|---|
| PHP 版本: | 4.0+ |
| PHP 更新日志: | PHP 4.3:添加了可选的 file 和 line 参数。 |