Reputation: 4042
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());
Upvotes: 21
Views: 36353
Reputation: 1908
Once the header is sent we can not clear it. The best solution for it is to turn on output_buffering in the php.ini file.
output_buffering = On
But if the header is not sent then you can clear it via header_remove(); function.
Upvotes: 1
Reputation: 3907
To remove them all it's pretty simple:
if ( ! headers_sent() ) {
header_remove();
}
No looping required. If you don't pass a parameter to header_remove
, it removes all headers set by PHP.
Upvotes: 13
Reputation: 239301
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);
}
Upvotes: 27
Reputation: 522135
You can remove the headers only if they're not already sent. If headers_sent
is true
, the headers have already gone out and you cannot unset them anymore.
Upvotes: 6