header_remove

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

header_remove以前に設定したHTTPヘッダを削除する

説明

function header_remove(?string $name = null): void

header()関数を使って以前に設定したHTTPヘッダを削除します。

パラメータ

name

削除するヘッダの名前。 null の場合は、以前に設定されたヘッダ全てが削除されます。

注意: このパラメータは大文字小文字を区別しません

戻り値

値を返しません。

変更履歴

バージョン 説明
8.0.0 name は、nullable になりました。

例1 特定のヘッダを削除する

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

上の例の出力は、 たとえば以下のようになります。

X-Bar: Baz

例2 以前設定したすべてヘッダを削除する

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

上の例の出力は、 たとえば以下のようになります。


注意

警告

この関数はPHPが設定した すべての ヘッダを削除します。 削除されるヘッダには、cookie や セッション、X-Powered-By があります。

注意: ヘッダにアクセスできたりヘッダを出力したりするのは、 それに対応した SAPI を使っている場合のみです。

参考

  • header() - 生の HTTP ヘッダを送信する
  • headers_sent() - ヘッダが既に送信されているかどうかを調べる

add a note

User Contributed Notes 5 notes

up
15
Saeed Khamseh
15 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
3
Anonymous
10 years ago
expose_php is php.ini only!

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

works:
header_remove('x-powered-by');
up
1
luis dot angelino at laweb dot com dot br
2 years ago
If you are using this:

#!/usr/local/bin/php

You can add "-q" at the end of it and the headers will be removed, beacuse header_remove will not remove "Content-type"

#!/usr/local/bin/php -q
up
0
razvan_bc at yahoo dot com
7 days ago
<?php
header_remove('Server');
phpinfo();
?>
In Apache php 8.5.6 it will only delete in the HTTP Headers Information section but the header is sent by Apache and cannot be override by php.
You can find it in the browser console, in the network section.
So you will need other various methods..
https://stackoverflow.com/questions/9000853/apache-how-to-hide-server-version-and-operation-system-from-users
up
-2
jake at qzdesign dot co dot uk
8 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
);
?>
To Top