问题
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());
回答1:
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);
}
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/9881410/clear-previously-set-headers-php