array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.headers-sent.php', 1 => 'headers_sent', ), 'up' => array ( 0 => 'ref.network.php', 1 => '网络 函数', ), 'prev' => array ( 0 => 'function.headers-list.php', 1 => 'headers_list', ), 'next' => array ( 0 => 'function.http-response-code.php', 1 => 'http_response_code', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/network/functions/headers-sent.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
headers_sent — 检测消息头是否已经发送
filename
若设置了可选参数 filename
和
line
,headers_sent() 会把 PHP 文件名放在
filename
变量里,输出开始的行号放在 line
变量里。
注意:
如果在执行 PHP 源文件之前已经开始输出(例如由于启动错误),则
filename
参数将被设置为空字符串。
line
输出开始的行号。
示例 #1 使用 headers_sent() 的例子
<?php
// 没有消息头就发送一个
if (!headers_sent()) {
header('Location: http://www.example.com/');
exit;
}
// 使用 file 和 line 参数选项的例子
// 注意 $filename 和 $linenum 用于下文中使用
// 所以不要提前为它们赋值
if (!headers_sent($filename, $linenum)) {
header('Location: http://www.example.com/');
exit;
// 很有可能在这里触发错误
} else {
echo "Headers already sent in $filename on line $linenum\n" .
"Cannot redirect, for now please click this <a " .
"href=\"http://www.example.com\">link</a> instead\n";
exit;
}
?>
注意:
数据头只会在SAPI支持时得到处理和输出。