header_register_callback
(PHP 5 >= 5.4.0, PHP 7, PHP 8)
header_register_callback — 呼叫一個 header 函式
說明
註冊一個函式,在 PHP 開始發送輸出時呼叫。
PHP 準備好所有響應頭,在發送內容之前執行 callback,建立了一個發送響應頭的操作視窗。
參數
-
callback -
在頭發送前呼叫函式。 它沒有參數,返回的值也會被忽略。
返回值
成功時返回 true, 或者在失敗時返回 false。
範例
示例 #1 header_register_callback() 例子
<?php
header('Content-Type: text/plain');
header('X-Test: foo');
function foo() {
foreach (headers_list() as $header) {
if (strpos($header, 'X-Powered-By:') !== false) {
header_remove('X-Powered-By');
}
header_remove('X-Test');
}
}
$result = header_register_callback('foo');
echo "a";
?>
以上例程的輸出類似於:
Content-Type: text/plain a
註釋
header_register_callback() 是在頭即將發送前執行的, 所以本函式的任意內容輸出都會打斷輸出過程。
注意:
數據頭只會在SAPI支援時得到處理和輸出。
參見
- headers_list() - 返回已發送的 HTTP 響應頭(或準備發送的)
- header_remove() - 刪除之前設定的 HTTP 頭
- header() - 發送原生 HTTP 頭