PHP Cannot modify header information

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/test.php:3) in /home/user/public_html/test.php on line 4

Look familiar? This PHP error has haunted website owners for years. Please understand, HostGator does not normally support your webdesign and coding, but this error is too common for us to ignore. So, one of our PHP experts has the answer for you.

The problem you face is that you are trying to use the PHP header function, header(), but there can be absolutely no HTML output before this function is declared. Let's use some examples:

<!DOCTYPE "HTML 4.01 Transitional EN" "http://www.w3.org/">
<html><body>
<?php header ('Location: http://hostgator.com/'); ?>
</body>
</html>
This is incorrect coding. You cannot have any HTML preceding the header function. A few more bad examples:
<?php
echo "Task complete.";
header ('Location: http://hostgator.com/');
?>
Task complete! <?php
header ('Location: http://hostgator.com/');
?>
This is incorrect coding for the same reason. You cannot have any text output before the header function. Plain text is always treated as HTML by your browser. One more bad example:
 <?php
header ('Location: http://hostgator.com/');
?>
This one is tricky. The blank space before the PHP tag counts as HTML output.

It is acceptable to have other PHP code before the header function, as long as nothing is outputted as HTML. This is a correct example:
<?php
$variable = "value";
mysql_query("blah blah");
header ('Location: http://hostgator.com/');
?>
I hope this helps. Remember, our technicians cannot fix your PHP code, so if you need more help, please refer to http://us.php.net/manual/en/index.php

Article Comments

Marilyn Dunne
I have read all of this but I cannot get back in to my admin area to fix it. I just get the error message and I need to know how to get in to that area as the error message blocks my entry.

HostGator
This is not something you can fix in your admin area. You must fix this in your page's source code (within File Manager or an FTP connection).

Please contact HostGator for assistance via phone or live chat. We may be able to help you solve this.

Nathan
The example provided does not work for me. It seems that no matter how I manipulate the code, redirecting a page using Header() always returns an error. The error always claims headers were sent in line 1, when in fact line 1 ENTIRELY consists of <?php and nothing else. For a web developer this is very frustrating for me seeming how this is such a simple thing and very important php function. Every other host I have ever used runs the function without error, whether it be before html or after.

HostGator
You likely have PHP auto_prepend_file set to something. You can fix this in you php.ini file.

If you still have the problem or need help making the change, simply contact HostGator and we will fix it for you.

Robert
So if no html can come before the php code, then what this means is that I can not put the form into an existing page? That I can't have my website header above the form? Not very realistic it seems.

HostGator
Only certain PHP code cannot be preceded by HTML, specifically the header() function.

You can easily have PHP code in the middle of your HTML code, as long as the header function is not used.

This EXAMPLE is valid coding:
<!DOCTYPE "HTML 4.01 Transitional EN" "http://www.w3.org/">
<html><body>
<?php echo "Hello, ".$name."!"; ?>
</body>
</html>

This EXAMPLE is also valid coding:
<?php
if($_POST['name']){
?>
<!DOCTYPE "HTML 4.01 Transitional EN" "http://www.w3.org/">
<html><body>
Success.
</body>
</html>
<?php
}else{
header('Location: http://hostgator.com/');
}
?>

This last one worked because the header() is not called when the IF statement is true. However, when the IF statement is false, the HTML code is skipped.


Your comments help us keep the knowledge base updated. This is not a medium for support. If you have questions or need help, please contact us via email, phone or live chat for fast assistance.

Post Comment