PHP header_register_callback() 函数用法及示例

PHP HTTP  参考手册

header_register_callback() 函数调用一个 header 函数。

语法

eader_register_callback ( callable $callback )

定义和用法

注册一个函数,在 PHP 开始发送输出时调用。
PHP 准备好所有响应头,在发送内容之前执行 callback,创建了一个发送响应头的操作窗口。

注意: header_register_callback() 是在头即将发送前执行的, 所以本函数的任意内容输出都会打断输出过程。

返回值

 成功时返回 TRUE, 或者在失败时返回 FALSE。

参数

序号参数和说明
1

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

PHP HTTP  参考手册