PHP 网络函数

PHP header_register_callback() 函数注册一个callback 回调函数。 回调函数在 PHP 准备好要发送的所有标头之后、发送任何其他输出之前执行。

语法

header_register_callback(callback) 

参数

callback必填。 指定回调函数。它在发送标头之前被调用。它没有参数,并且返回值被忽略。

返回值

成功时返回 true,失败时返回 false。 

示例:header_register_callback() 示例

下面的示例显示了header_register_callback() 函数的用法。

<?php
//指定响应中的纯文本内容
header('Content-Type: text/plain');

//定义响应头
header('X-Test: foo');

function myfunc() {
  foreach (headers_list() as $header) {
    //如果包含"X-Powered-By:"则删除标头
    if (strpos($header, 'X-Powered-By:') !== false) {
      header_remove('X-Powered-By');
    }

    //删除"X-Test"标头
    header_remove('X-Test');
  }
}

$result = header_register_callback('myfunc');
echo "Hello";
?> 

上述代码的输出将是:

Content-Type: text/plain

Hello