Sunday, 15 June 2014

Cannot modify header information - headers already sent

The “Cannot modify header information – headers already sent” error is incredibly common, and I receive many many questions from all over about what it is and what caused it. So let’s clear all of this up once and for all.Upon doing a Google search for this error, I found many helpful articles, but none that were quite helpful enough – they give tips, but some of the details were a bit fuzzy.


But there are lots of useful solutions for this problem -


Echo before header


<?php
echo "hello world"
...
header('Location: http://...');
?>

 


Solution:


RemoveRemove the echo before header

Use ob_start(); and ob_end_flush(); for buffered output


New-line characters or spaces before <?php


 <?php
... header('Location: http://...');
?>

 


Solution:


Remove everything before <?php


Header Location in mixed php/HTML


<html>
...
<body>
...
<?php header('Location: http://...'); >
...
</body>
</html>

 


Sorry, you cannot use header-location here, because the header and the HTML code have already been sent!


 


Empty lines, chars or spaces after ?> when using an php include file


<?php
...
?>

...
...

 


Solution:


Remove everything after ?> in the php include file


or


This normally can be resolved by a quick custom php.ini alteration:


1) Create a custom php.ini file (if there isn’t one already)


For information on how to create a custom php.ini file, please use other source.


2) Locate the line that looks like:



output_buffering = Off


3) Change the output_buffering line to:



output_buffering = On


This should be all that is needed to bring the site back online.



Cannot modify header information - headers already sent

No comments:

Post a Comment