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.w3cschool.cc/");
exit;
}
?>
<html>
<body>
...
...
// If no headers are sent, send one
if (!headers_sent())
{
header("Location: http://www.w3cschool.cc/");
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.w3cschool.cc/");
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.w3cschool.cc/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
完整的PHP HTTP參考手冊