PHP Header Location Error- Warning: Cannot Modify Header Information- Headers Already Sent

PHP


So sometimes when using the PHP header function to cause a redirect to another page, you will get the error:

Warning: Cannot Modify Header Information- Headers Already Sent by (output started at /home/content/84/.../searchpage.php:1)

This can happen such as with using the following code:

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

This error occurs for pretty much one reason only but how it can exist can take many forms. We'll explain now.

The reason this error occurs is because output is written to the browser before the redirect to another page occurs. With the PHP header function, this is forbidden. No output can be written to the browser before the redirect occurs. If this is the case, you will get the error, such as shown above.

So how does output can written to the browser?

And the answer is, this can take many forms.

For one, you may actually have output shown before the php header function, such as an echo statement.

The code below will run an error:

<?php
echo "This output causes the header function to not work";
header('Location: http://www.google.com');
?>


The above is a more obvious blatant example.

However if you have an HTML tag located before the code, it will also cause this error to occcur. This html tag may be <html> or <header>.

The code below will cause an error:

<?php
<html> header('Location: http://www.google.com');
?>


So you can't have any statements printed by PHP. You can't have any HTML tags present before the header function.

You also can't even have white space present before the PHP header tag. This, too, will cause an error.

The following code below will cause an error:

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


Only the following code, below, no white space, html tags, or statements printed to output will cause no errors.

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


But what if you've run the code with absolute no white space, html tags, printed statements. The php code is right at the top with only the header function, and yet the same error is still occurring. Then what may be causing it? Yes, even with absolutely no space visible to us, this error can still occur.

And if you have no whitespace or anything else and still getting the error, it's more than likely due to how the PHP file is saved. If you use a text editor like notepad, you can save PHP file in many different encoding formats. You can save the PHP file in either ANSI encoding, Unicode encoding, Unicode big endian encoding, or UTF-8 encoding. The format you choose can trigger this error. The best encoding to save it at is the ANSI encoding.

ANSI encoding


This encoding adds no hidden whitespace or characters to the PHP file. If you use unicode, unicode big endian, or UTF-8 encoding, these encodings can actually add characters to the text in the PHP file. This is why this error can occur even when you see absolutely no whitespace at all. Some encodings can add things to the file you can't even see. The best format to save the file is in ANSI. It's the purest form that doesn't add characters to the PHP file, so what you actually can see in the text is the same behind the scenes of the binary code. The other encodings can add. So whatever you have for the PHP, copy it, and paste it to notepad. Save it with a .php extension and in ANSI encoding. Then upload this file to web and this may correct the error.

So there are many reasons why this error may occur again, but these solutions should fix that problem.

To see a classic example of a program that uses the header function that uses location for a redirect, see How to Create a Search Engine Using PHP. We create a search engine using the php function as the basis of the search engine. This function causes redirects to various websites based on the search query that a user enters.


HTML Comment Box is loading comments...