PHP header_register_callback Function
last modified April 4, 2025
The PHP header_register_callback
function registers a callback
that will be called when headers are sent. It's useful for header manipulation.
Basic Definition
header_register_callback
registers a function to be called
just before PHP starts sending output. The callback can modify headers.
Syntax: header_register_callback(callable $callback): bool
.
Returns true on success, false on failure. Requires output buffering.
Basic Header Callback Example
This example shows a simple callback that adds a custom header.
<?php declare(strict_types=1); function headerCallback() { header('X-Custom-Header: MyValue'); } header_register_callback('headerCallback'); echo "Content with custom header";
The callback adds an X-Custom-Header before output starts. This demonstrates the basic usage of header_register_callback. The callback runs automatically.
Conditional Header Modification
This example conditionally modifies headers based on application logic.
<?php declare(strict_types=1); function conditionalHeader() { if ($_SERVER['REMOTE_ADDR'] === '127.0.0.1') { header('X-Debug-Mode: enabled'); } } header_register_callback('conditionalHeader'); echo "Content with conditional headers";
The callback checks the client IP and adds a debug header for localhost. This shows how to make dynamic header decisions before output begins.
Multiple Callbacks Example
This demonstrates registering multiple callbacks that execute in order.
<?php declare(strict_types=1); function firstCallback() { header('X-First: 1'); } function secondCallback() { header('X-Second: 2'); } header_register_callback('firstCallback'); header_register_callback('secondCallback'); echo "Content with multiple callbacks";
Callbacks execute in registration order. This example adds two custom headers. The order of registration determines the execution sequence of callbacks.
Header Validation Callback
This example validates headers before they're sent using a callback.
<?php declare(strict_types=1); function validateHeaders() { $headers = headers_list(); if (!in_array('Content-Type: text/html', $headers)) { header('Content-Type: text/html'); } } header_register_callback('validateHeaders'); echo "<html><body>Validated content</body></html>";
The callback checks existing headers and adds missing ones. This ensures required headers are present before output starts. Useful for validation.
Output Buffering with Callback
This shows how output buffering works with header_register_callback.
<?php declare(strict_types=1); ob_start(); function bufferingCallback() { header('X-Buffered: yes'); header_remove('X-Powered-By'); } header_register_callback('bufferingCallback'); echo "Buffered content with modified headers"; ob_end_flush();
Output buffering must be active for header callbacks to work. This example shows buffering setup and header modification. Callbacks run before flush.
Best Practices
- Output Buffering: Always use output buffering with this function
- Single Responsibility: Keep callbacks focused on one task
- Order Matters: Register callbacks in execution order needed
- Error Handling: Handle potential callback errors gracefully
Source
PHP header_register_callback Documentation
This tutorial covered the PHP header_register_callback
function
with practical examples for header manipulation in various scenarios.