PHP header_register_callback() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, header_register_callback() registers a function that runs just before PHP sends the response headers. Your callback can still inspect or change headers in that short window.

Only one callback is active at a time. A later call silently replaces the previous callback. The callback receives no arguments, and its return value is ignored.

Syntax

Call header_register_callback() with the values you need. Start with this line.

PHP
$ok = header_register_callback(function () { /* adjust headers */ });
CallWhat it does
header_register_callback("tidyHeaders")Registers a named function to run before headers are sent.
header_register_callback(function () { header_remove("X-Powered-By"); })Removes a header at the last moment.
header_register_callback(fn() => null)Registers a tiny callback; returns true on success.

The callback runs after headers are prepared and before body output. Do not print from the callback, or you can break the response. Callbacks are not stacked.

Library declarations
PHP
header_register_callback(callable $callback): bool

Parameters

ParameterDescription
callbackRequired. A callable with no parameters. It runs just before headers are sent.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Add a Header From the Callback

Register a callback that queues one more header, then list headers as a web SAPI would show them.

PHP
<?php
header("Content-Type: text/plain");

header_register_callback(function () {
    header("X-Ready: yes");
});

// In a real response, output would flush headers and run the callback.
foreach (["Content-Type: text/plain", "X-Ready: yes"] as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. Content-Type is set before the callback is registered.
  2. The callback adds X-Ready just before headers would be sent.
  3. The printed lines show the combined header set a web SAPI would expose.
  4. CLI demos often cannot flush real headers, so the listing is illustrative.

Example 2 — Remove a Header at the Last Moment

Queue a temporary header, then remove it inside the registered callback.

PHP
<?php
header("X-Temp: remove-me");
header("X-Keep: stay");

header_register_callback(function () {
    header_remove("X-Temp");
});

foreach (["X-Keep: stay"] as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. X-Temp and X-Keep are both queued first.
  2. The callback calls header_remove("X-Temp") before send.
  3. Only X-Keep remains in the illustrative listing.
  4. This pattern is useful for stripping headers you do not want clients to see.

Example 3 — Later Callback Replaces the Earlier One

Register two callbacks and show that only the last one stays active.

PHP
<?php
$which = "none";

header_register_callback(function () use (&$which) {
    $which = "first";
});
header_register_callback(function () use (&$which) {
    $which = "second";
});

// Simulate the final callback running:
$which = "second";
echo "active callback: ", $which, PHP_EOL;

How It Works

  1. The first registration would set $which to first.
  2. The second registration silently replaces the first callback.
  3. Only the second callback would run when headers are sent.
  4. Remember that callbacks are not stacked like shutdown functions.

Frequently Asked Questions

It registers one function that PHP calls just before sending response headers, so you can still change headers at the last moment.
No. Only one is kept. Each new call replaces the previous callback.
No. It is called with no parameters, and its return value is ignored.
Use it for last-second cleanup, such as removing X-Powered-By or adding a request id header, when you cannot control every earlier header() call.
Avoid printing. Output from the callback can break the response because headers are about to be sent.

Next: header_remove()

header_remove() deletes a previously queued response header by name. If you omit the name,.

header_remove() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful