header_remove

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

header_removeSupprime un en-tête HTTP

Description

header_remove(?string $name = null): void

Supprime un en-tête HTTP précédemment ajouté avec header().

Liste de paramètres

name

Le nom de l'en-tête à supprimer. Si null tout les en-têtes définit précédemment sont supprimés.

Note: Ce paramètre est insensible à la casse.

Valeurs de retour

Aucune valeur n'est retournée.

Historique

Version Description
8.0.0 name est désormais nullable.

Exemples

Exemple #1 Supprimer un en-tête HTTP avec header_remove()

<?php
header
("X-Foo: Bar");
header("X-Bar: Baz");
header_remove("X-Foo");
?>

Résultat de l'exemple ci-dessus est similaire à :

X-Bar: Baz

Exemple #2 Supprimer tous les en-têtes HTTP avec header_remove()

<?php
header
("X-Foo: Bar");
header("X-Bar: Baz");
header_remove();
?>

Résultat de l'exemple ci-dessus est similaire à :


Notes

Attention

Cette fonction va supprimer tous les en-têtes configurés par PHP, y compris les cookies, les sessions et les en-têtes X-Powered-By.

Note:

Les en-têtes ne seront accessibles et s'afficheront que lorsqu'un SAPI qui les supporte sera utilisé.

Voir aussi

  • header() - Envoie un en-tête HTTP brut
  • headers_sent() - Indique si les en-têtes HTTP ont déjà été envoyés

add a note add a note

User Contributed Notes 4 notes

up
13
Saeed Khamseh
13 years ago
if you want to remove header information about php version (x-powered-by), you can use:

header_remove('x-powered-by');

alternatively, if you don't have php 5.3 installed, you can do the same thing using "header" command:

header('x-powered-by:');

don't forget the ':' character at the end of the string!
up
2
jake at qzdesign dot co dot uk
5 years ago
When called from a command-line process, this function does nothing when passed a specific header to remove, but it does nonetheless work properly when called with no arguments to remove all headers.

Thus, when unit-testing or executing in some other test harness, if the code you are testing may call `header_remove()`, with the UOPZ and XDebug extensions loaded, you could use the following in order to more effectively test that the expected headers are set [which you would do by inspecting the array returned by `xdebug_get_headers()` after running the code under test, as `headers_list()` does not work despite the headers actually being stored internally as normal]:

<?php
uopz_set_return
(
 
'header_remove',
  function(
$name = null) {
    if (
$name !== null) {
     
$pattern = '/^' . preg_quote($name, '/') . ':/i';
     
$headers = array_filter(
       
xdebug_get_headers(),
        function(
$header) use($pattern) {
          return !
preg_match($pattern, $header);
        }
      );
    }
   
// This works to remove all headers, just not individual headers.
   
header_remove();
    if (
$name !== null) {
      foreach (
$headers as $header) {
       
header($header);
      }
    }
  },
 
true
);
?>
up
3
Anonymous
8 years ago
expose_php is php.ini only!

this won't work:
ini_set('expose_php',0);

works:
header_remove('x-powered-by');
up
-9
J W
9 years ago
You should ini_set("expose_php", 0); or set it in your php.ini to Off, instead of using this to remove X-Powered-By header.
To Top