Clear previously set headers php

前端 未结 4 912
忘掉有多难
忘掉有多难 2021-02-05 16:15

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){
                


        
相关标签:
4条回答
  • 2021-02-05 16:38

    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);
    }
    
    0 讨论(0)
  • 2021-02-05 16:41

    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
    
    0 讨论(0)
  • 2021-02-05 16:48

    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.

    0 讨论(0)
  • 2021-02-05 16:52

    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.

    0 讨论(0)
提交回复
热议问题