Why the “Header cannot be Modified” error happens?
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Example :
Let’s say you are writing a PHP script that will redirect to another page
This code will cause the error (because of the blank line above the header output)
1:
2: <?php
3: header(”Location: welcome.php”); /* Redirect browser */
4:
5: exit;
6: ?>
This code will also cause the error (because of the html output which is above the header call)
1: <html>
2: <?php
3: header(’Location: welcome.php’);
4: ?>
This code will work
1: <?php
2: header(”Location: welcome.php”); /* Redirect browser */
3:
4: exit;
5: ?>
