PHP Function Reference

PHP header_register_callback() Function



The PHP header_register_callback() function registers a callback function. The callback is executed just after PHP prepares all headers to be sent, and before any other output is sent, creating a window to manipulate the outgoing headers before being sent.

Syntax

header_register_callback(callback)

Parameters

callback Required. Specify a callback function. It is called just before the headers are sent. It gets no parameters and the return value is ignored.

Return Value

Returns true on success or false on failure.

Example: header_register_callback() example

The example below shows the usage of header_register_callback() function.

<?php
//specifing plain text content in the response
header('Content-Type: text/plain');

//defining the response header
header('X-Test: foo');

function myfunc() {
  foreach (headers_list() as $header) {
    //removing headers if contains 'X-Powered-By:'
    if (strpos($header, 'X-Powered-By:') !== false) {
      header_remove('X-Powered-By');
    }

    //removing 'X-Test' header
    header_remove('X-Test');
  }
}

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

The output of the above code will be:

Content-Type: text/plain

Hello

❮ PHP Network Reference