PHP headers_sent()関数
例
ヘッダーが送信されない場合は、次のヘッダーを送信します。
<?php
if (!headers_sent()) {
header("Location: https://www.w3schools.com/");
exit;
}
?>
<html>
<body>
...
...
定義と使用法
headers_sent()関数は、ヘッダーが送信されたかどうか/どこに送信されたかを確認します。
構文
headers_sent(file,line)
パラメータ値
Parameter | Description |
---|---|
file | Optional. If the file and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the file and line variables |
line | Optional. Specifies the line number where the output started |
技術的な詳細
戻り値: | HTTPヘッダーが送信された場合はTRUE、それ以外の場合はFALSE |
---|---|
PHPバージョン: | 4.0以降 |
PHP変更ログ: | PHP 4.3:オプションのファイルと行のパラメーターを追加しました |
その他の例
例
オプションのファイルおよび行パラメーターの使用:
<?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>
...
...
❮PHPネットワークリファレンス