PHP Warning: Cannot modify header information – headers already sent

Message:

PHP Warning: Cannot modify header information - headers already sent by (output started at /home/w.php:189) in /home/start.php on line 64

Error type: warning

Symptoms:

You try to send a header value from PHP (for example a redirect) and it fails with this warning.

Sample Code:

<?php
echo ‘Redirecting: ‘;
header(‘Location: /’);
exit;
?>

Cause:

You must send headers before you output anything on that page. This means you can’t use echo, printf or any other output function before all headers are sent.

Fix:

Check where you used the header() function and remove any output instructions before that.

Please note that blank spaces before <?php are also considered part of the output and will give you lots of trouble. This also applies to files you include (for example configuration files), so make sure you don’t leave extra spaces/empty lines).

Another method to get rid of this problem is to use output buffering.

2 Responses

  1. admin Says:

    In that example you should first send the header() and THEN the echo.

  2. vivichie Says:

    Thanks! your explanations are clear and helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *