清除以前设置的标题php

时间:2012-03-26 23:37:48

标签: php http-headers

我想知道是否可以清除存储在header_list()

中的当前信息
if(headers_sent()){
    foreach(headers_list() as $header){
        header_remove($header);
    }
}
var_dump(headers_list());

4 个答案:

答案 0 :(得分:18)

headers_sent表示删除标题为时已晚。他们已经已发送。因此,函数的名称。

您想要的是专门检查标题是否已发送 。然后你知道修改它们是安全的。

if (!headers_sent()) {
  foreach (headers_list() as $header)
    header_remove($header);
}

答案 1 :(得分:6)

只有在尚未发送标题时才能删除标题。如果headers_senttrue,则标题已经消失,您无法再将其取消设置。

答案 2 :(得分:2)

要删除它们,这很简单:

if ( ! headers_sent() ) {
    header_remove();
}

不需要循环。如果您没有将参数传递给header_remove,它将删除PHP设置的所有标头。

答案 3 :(得分:0)

一旦发送了标头,我们将无法清除它。最好的解决方案是在php.ini文件中打开output_buffering。

    #include <vector>
    #include <string>
    #include <array>
    #include <iostream>

    int main()
    {
        std::vector<float> grades;
        const std::vector<std::string>  subjects = {
            "Chemistry for Engineers Laboratory",
            "Chemistry for Engineers", 
            "Computer Aided Drafting",
            "Calculus",
            "Kontekstwalisadong Komunikasyon sa Filipino at sa Iba't-ibang Larangan"
            //Extend the subjects
        };

        int TotlaSubject = subjects.size();

        std::cout << "\n Enter your grades on each subject\n";
        std::cout << "-----------------------------------------------\n";
        for (int i = 0; i < TotlaSubject; i++)
        {
            std::cout << subjects[i] << " : ";
            float InputGrade;
            std::cin >> InputGrade;
            grades.push_back(InputGrade);
//          ^^^^^ here pushing data into the in a loop
        }

        float total =0.0;
        for (auto grade : grades){
            total += grade;  //Adding grades in a loop
         }

        float avg = total / grades.size();
        std::cout << "Total : " << total << " Avg : " << avg << std::endl;        
        return 0;
    }