PHP headers_sent() 函式
示例
如果沒有傳送任何頭資訊,則傳送一個
<?php
if (!headers_sent()) {
header("Location: https://w3schools.tw/");
exit;
}
?>
<html>
<body>
...
...
定義和用法
headers_sent() 函式檢查是否/在哪裡傳送了頭資訊。
語法
headers_sent(file,line)
引數值
引數 | 描述 |
---|---|
file | 可選。如果設定了 file 和 line 引數,headers_sent() 將把輸出開始的 PHP 原始檔名和行號放入 file 和 line 變數中 |
line | 可選。指定輸出開始的行號 |
技術詳情
返回值 | 如果 HTTP 頭已傳送,則返回 TRUE,否則返回 FALSE |
---|---|
PHP 版本 | 4.0+ |
PHP 更新日誌 | PHP 4.3:添加了可選的 file 和 line 引數 |
更多示例
示例
使用可選的 file 和 line 引數
<?php
// $file 和 $line 被傳入以供以後使用
// 不要預先為它們賦值
if (!headers_sent($file, $line))
{
header("Location: https://w3schools.tw/");
exit;
// 在此處觸發錯誤
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
❮ PHP 網路參考