PHPのheaders_sent()関数
定義と使用法
HTTPヘッダが送信/に送信されたかどうかheaders_sent()関数をチェックします。
ヘッダが送信された場合、この関数はそれ以外の場合はFALSEを返し、TRUEを返します。
文法
headers_sent(file,line)
参数 | 描述 |
---|---|
file,line | 可选。如果设置 file 和 line 参数,headers_sent() 会把输出开始的 PHP 源文件名和行号存入 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
オプションのファイルと行パラメータを使用します:
<?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リファレンスマニュアル