Clear previously set headers php
Once header is sent we can not clear it. The best solution for it to turn on output_buffering in php.ini file.
output_buffering = On
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.
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);
}
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.