PHP headers_sent()関数
完全な PHP HTTP リファレンスマニュアル
定義と使用法
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 リファレンスマニュアル その他の拡張機能