Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Network header_remove() Function



The PHP Network header_remove() function is used to remove HTTP headers that were previously set using the header() method. It is available starting with PHP 5.3.0 and works with both PHP 7 and PHP 8. You can remove a specific header by giving its name as an input. Without a name, it will remove all previously set headers.

The header name is not case-sensitive, so you can use capital or lowercase letters. This function does not return any value. When you need to update or clean up headers before replying to a user, it is helpful.

Syntax

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

void header_remove ( ?string $name = null )

Parameters

This function accepts $name parameter which is a name of the header should be deleted. All previously set headers are deleted when null.

Return Value

The header_remove() function does not return any value.

PHP Version

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

Example 1

First we will show you the basic example of the PHP Network header_remove() function to remove a specific HTTP header set earlier. It makes sure only the given header is removed.

<?php
   // Set multiple headers
   header("Content-Type: text/html");
   header("X-Powered-By: PHP");

   // Remove the "X-Powered-By" header
   header_remove("X-Powered-By");
   echo "Header removed successfully.";
?>

Output

Here is the outcome of the following code −

Header removed successfully.

Example 2

In the below PHP code we will use the header_remove() function to remove all previously set HTTP headers by calling the function without any argument. So it is useful for resetting headers entirely.

<?php
   // Set multiple headers
   header("Content-Type: text/html");
   header("X-Powered-By: PHP");

   // Remove all headers
   header_remove();
   echo "All the Headers removed successfully.";
?> 

Output

This will generate the below output −

All the Headers removed successfully.

Example 3

Now the below program uses header_remove() in a case where the headers are reset before sending a custom response to the user.

<?php
   // Set initial headers
   header("Content-Type: application/json");
   header("X-Powered-By: PHP");

   // Reset all headers before sending a response
   header_remove();

   // Set new headers
   header("Content-Type: text/plain");
   echo "Headers have been reset and updated.";
?> 

Output

This will create the below output −

Headers have been reset and updated.

Note: All PHP-set headers, including cookies, sessions, and the X-Powered-By headers, will be eliminated by this function. Only when a SAPI that supports headers is in use will they be available and produced.

php_function_reference.htm
Advertisements