(PHP 3 >= 3.0.8, PHP 4, PHP 5)
headers_sent -- 檢查標頭是否已被傳送以及在哪裡被傳送
說明
bool
headers_sent ( [string &file [, int &line]] )
若果 HTTP 標頭尚未被傳送出去的話,headers_sent()
將返回 FALSE,否則返回 TRUE。若果指定了可選參數 file 和
line, headers_sent()
將會把 PHP 的檔案名以及從哪一行開始有輸出放到 file
和 line 變量中。
一旦標頭已經被傳送,將不能再使用
header() 函數來傳送其它的標頭。使用此函數至少可以避免避免與
HTTP 標頭有關的錯誤訊息。另一個選取是使用輸出暫存。
注:
可選參數 file 和 line
是 PHP 4.3.0 版增加的。
例子 1. headers_sent() 例子
<?php
// 若果尚未傳送標頭,傳送一個 if (!headers_sent()) { header('Location: http://www.example.com/'); exit; }
// 一個使用可選參數 file 和 line 的例子,自 PHP 4.3.0 起 // 注意 $filename 和 $linenum 被傳遞進來用於後續代碼,不要預先賦值 if (!headers_sent($filename, $linenum)) { header('Location: http://www.example.com/'); exit;
// 這裡就觸發了一個錯誤 } else {
echo "Headers already sent in $filename on line $linenum\n" . "Cannot redirect, for now please click this <a " . "href=\"http://www.example.com\">link</a> instead\n"; exit; }
?>
|
|
關聯問題的詳細討論參見 ob_start(),trigger_error()
和 header()。