Showing posts with label headers_sent. Show all posts
Showing posts with label headers_sent. Show all posts

Monday, September 25, 2017

Clear previously set headers php | How to fix “Headers already sent” error in PHP

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.


<?php
header('Content-type: application/csv');

if (!headers_sent()) {
    echo "<pre>";
    print_r(headers_list());
    echo "</pre>";
}

header_remove();
header('Content-type: text/html');
echo "HERE";


Array
(
    [0] => X-Powered-By: PHP/7.0.9
    [1] => Content-type: application/csv
)
HERE