PHP Network header_register_callback() Function



The PHP Network header_register_callback() function is used to write a custom function that runs just before headers are sent to the browser. This feature is supported by PHP versions 5.4.0 and up, including PHP 7 and PHP 8.

PHP will call the callback function you registered when it is ready to send headers but has not sent any output yet. This allows you to make last-minute changes to the headers before they are sent.

The function you provide as the callback does not accept any parameters and does not use its return value. If the setup is successful, the function returns true. If there is a problem, it returns false.

Syntax

Below is the syntax of the PHP Network header_register_callback() function −

bool header_register_callback ( callable $callback )

Parameters

This function accepts $callback parameter which is a function that is carried out right before the headers are sent. It gets no parameters and disregards the return value.

Return Value

The header_register_callback() function returns TRUE on success. And FALSE on failure.

PHP Version

First introduced in core PHP 5.4.0, the header_register_callback() function continues to function easily in PHP 7, and PHP 8.

Example 1

This program registers a basic callback function that generates a log message when headers are received. It shows the most basic usage of the PHP Network header_register_callback() function.

<?php
   // Callback function to log a message
   function logHeaders() {
      error_log("Headers are being sent.");
   }

   // Registering the callback
   header_register_callback('logHeaders');

   // Sending a header
   header("Content-Type: text/plain");
   echo "Hello, World!";
?>

Output

Here is the outcome of the following code −

Headers are being sent.
Hello, World!

Example 2

This program inserts a custom header dynamically before headers are transmitted using the header_register_callback() function. It shows how to modify the headers of the callback function.

<?php
   // Callback function to add a custom header
   function addCustomHeader() {
      header("X-Custom-Header: MyValue");
   }

   // Registering the callback
   header_register_callback('addCustomHeader');

   // Sending a normal header
   header("Content-Type: text/plain");
   echo "Custom Header Added!";
?> 

Output

This will generate the below output −

Custom Header Added!

Example 3

Now in the below code we will use the header_register_callback() function with conditional header manipulation. This program shows how to modify conditional headers based on specific standards, like adding a different header if the user is an administrator.

<?php
   // Callback function 
   function conditionalHeader() {
      $isAdmin = true; 
      if ($isAdmin) {
         header("X-Admin-Access: Granted");
      } else {
         header("X-Admin-Access: Denied");
      }
   }

   // Registering the callback
   header_register_callback('conditionalHeader');

   // Sending a response
   header("Content-Type: text/plain");
   echo "Header based on condition!";
?> 

Output

This will create the below output −

Header based on condition!
php_function_reference.htm
Advertisements