Handle â€cannot modify header information – headers already sent byâ€

Summary

PHP developers often encounter the "Cannot modify header information – headers already sent by…" warning when output precedes header modifications. To address this common issue, developers should first inspect files for any blank lines or whitespace after the closing `?>` tag, especially in included or required scripts. Another effective solution involves adding an `exit()` statement immediately after a `header()` call to halt further execution. Alternatively, utilizing output buffering by calling `ob_start()` at the script's beginning and `ob_end_flush()` before sending headers can prevent premature output.

Man PHP developers should encounter ”Warning: Cannot modify header information – headers already sent by ….” error before when executing a PHP script.

Here are some solutions to this error.

1. Blank line

Check whether there is any blank line after , especially in the files which are used in include() and require(0, some problems are caused by blank lines.

2. Add exit() after head()

header (“Location: xxx”);
exit();

3. Use output cache

<?php ob_start(); ?>
… HTML codes …
<?php
… PHP codes …
header (“Location: ….”);
ob_end_flush();
?>

If you have any other solution to this error. You can share with us by post your solution to the comment area.

Source :http://blog.vip7758.com/63.html

HEADER SENT PHP ERROR HANDLE

  RELATED

  COMMENTS

2
leo
Jun 27, 2013 at 7:45 pm

The end of the first three examples to write a sh ();?> Right?

----From google translate

Night walker
Jun 27, 2013 at 9:33 pm

Hi, leo. Thank you for pointing out the error. We have modified the code.