PHP headers_sent() 函數(shù)
完整的 PHP HTTP 參考手冊
定義和用法
headers_sent() 函數(shù)檢查 HTTP 報頭是否發(fā)送/已發(fā)送到何處。
如果報頭已發(fā)送,該函數(shù)返回 TRUE,否則返回 FALSE。
語法
參數(shù) | 描述 |
file,line | 可選。如果設(shè)置 file 和 line 參數(shù),headers_sent() 會把輸出開始的 PHP 源文件名和行號存入 file 和 line 變量中。 |
提示和注釋
注釋:一旦報頭塊已經(jīng)發(fā)送,您就不能使用 header() 函數(shù)來發(fā)送其它的報頭。
注釋:可選的 file 和 line 參數(shù)是 PHP 4.3 中新增的。
實(shí)例 1
<?php
// If no headers are sent, send one
if (!headers_sent())
{
header("Location: http://www.jyygyx.com/s/");
exit;
}
?>
<html>
<body>
...
...
實(shí)例 2
使用可選的 file 和 line 參數(shù):
<?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.jyygyx.com/s/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
完整的 PHP HTTP 參考手冊