清除以前设置的标题 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9881410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear previously set headers php
提问by Killrawr
I would like to know if its possible to clear the current information stored in header_list()
我想知道是否可以清除存储在 header_list() 中的当前信息
if(headers_sent()){
foreach(headers_list() as $header){
header_remove($header);
}
}
var_dump(headers_list());
回答by meagar
headers_sent
indicates that it is too late to remove headers. They're already sent. Hence the name of the function.
headers_sent
表示删除标题为时已晚。他们已经发送了。因此,函数的名称。
What you want is to specifically check if the headers have notbeen sent yet. Then you know it's safe to modify them.
你需要的是专门检查标头没有被又发了。然后你知道修改它们是安全的。
if (!headers_sent()) {
foreach (headers_list() as $header)
header_remove($header);
}
回答by deceze
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.
仅当尚未发送标头时,您才能删除标头。如果headers_sent
是true
,则标头已经消失,您无法再取消设置它们。
回答by Stephen R
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.
不需要循环。如果您不向 传递参数header_remove
,它将删除由 PHP 设置的所有标头。
回答by Muzaffar Mahmood
Once header is sent we can not clear it. The best solution for it to turn on output_buffering in php.ini file.
一旦发送标头,我们就无法清除它。在 php.ini 文件中打开 output_buffering 的最佳解决方案。
output_buffering = On