Clear previously set headers php

扶醉桌前 提交于 2019-12-20 12:35:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!