Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear previously set headers php

I would like to know if its possible to clear the current information stored in header_list()

if(headers_sent()){
    foreach(headers_list() as $header){
        header_remove($header);
    }
}
var_dump(headers_list());
like image 544
classicjonesynz Avatar asked Mar 26 '12 23:03

classicjonesynz


People also ask

How to remove previous headers in php?

if you want to remove header information about php version (x-powered-by), you can use: header_remove('x-powered-by');

How can you remove the previous set header before sending the new headers?

The header_remove() function removes an HTTP header previously set with the header() function.

How to hide header in php?

There is an easy way to hide the PHP version from the HTTP headers. By setting the “expose_php” variable to Off in your php. ini file the PHP version would not longer be added to the HTTP headers.

How can I get header in php?

The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request. Parameters: This function accepts three parameters as mentioned above and described below: $url: It is a mandatory parameter of type string.


1 Answers

headers_sent indicates that it is too late to remove headers. They're already sent. Hence the name of the function.

What you want is to specifically check if the headers have not been sent yet. Then you know it's safe to modify them.

if (!headers_sent()) {
  foreach (headers_list() as $header)
    header_remove($header);
}
like image 82
meagar Avatar answered Oct 27 '22 17:10

meagar