header_register_callback

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

header_register_callback調(diào)用一個 header 函數(shù)

說明

header_register_callback(callable $callback): bool

注冊一個函數(shù),在 PHP 開始發(fā)送輸出時調(diào)用。

PHP 準備好所有響應(yīng)頭,在發(fā)送內(nèi)容之前執(zhí)行 callback,創(chuàng)建了一個發(fā)送響應(yīng)頭的操作窗口。

參數(shù)

callback

在頭發(fā)送前調(diào)用函數(shù)。 它沒有參數(shù),返回的值也會被忽略。

返回值

成功時返回 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() 是在頭即將發(fā)送前執(zhí)行的, 所以本函數(shù)的任意內(nèi)容輸出都會打斷輸出過程。

注意:

數(shù)據(jù)頭只會在SAPI支持時得到處理和輸出。

參見

  • headers_list() - 返回已發(fā)送的 HTTP 響應(yīng)頭(或準備發(fā)送的)
  • header_remove() - 刪除之前設(shè)置的 HTTP 頭
  • header() - 發(fā)送原生 HTTP 頭