headers_sent() 関数は、HTTPヘッダが送信されたかどうかをチェックします。
この関数は、ヘッダが送信されていれば TRUEを、そうでなければFALSEを返します。
headers_sent(file,line)
パラメータ | 説明 |
---|---|
file,line | 任意。file と line パラメータが設定されている場合、headers_sent()は、PHP のソースファイル名と出力が開始された行番号を、 それぞれ file と line 変数に格納する |
注: ヘッダ・ブロックをすでに送信してしまった後では、header()を使用してヘッダ行を追加することはできません。
注: オプションの file と line パラメータはPHP 4.3で追加されました。
<?php
// If no headers are sent, send one
if (!headers_sent())
{
header("Location: https://www.w3schools.com/");
exit;
}
?>
<html>
<body>
...
...
オプションの 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: https://www.w3schools.com/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...