Why the “Header cannot be Modified” error happens?

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: ?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.