You generally receive a:

Cannot modify header information - headers already sent by...

when a php script returns output to the screen (such as html code) and then tries to modify any of the header information. Headers should always be sent before any other ouput (such as html), and php will throw an error message if headers are sent after any other data is sent.

In this article, we'll provide two generic examples to show what is happening.

Example 1:

<?php

echo " THIS IS A TEST ";
header("Location:http://www.webhostinghub.com/support");

?>
In this example, it is easy to see that we are sending header information AFTER we send other data, which is wrong. This page will result in the following error message:

THIS IS A TEST
Warning: Cannot modify header information - headers already sent by (output started at /home/whhsup5/public_html/projects/cannot-modify-headers/1.php:4) in /home/whhsup5/public_html/projects/cannot-modify-headers/1.php on line 6


As you can see, "THIS IS A TEST" printed to the screen, and the error occurred after we then attemped to modify the header information using the header function on the next line. To resolve this issue, the header function call should be made first: 

<?php

header("Location:http://www.webhostinghub.com/support");
echo " THIS IS A TEST ";

?>

Example 2:

In example 2, we have two files:

2.php

<?php

// do something here, such as validate a login attempt

?>

3.php

<?php

// 2.php validates the user and checks if they logged in successfully
include_once("2.php");

header("Status: 403 Forbidden");

echo "<h1>This is custom 403 test page</h1>";

?>

In this example, 2.php includes the logic that would validate a login attempt, and 3.php would take action. It is a very simply

Did you find this article helpful?

Login to comment.

Your Opinion Matters

... but we need to know what you're thinking!

The Community Support team wrote the article you're looking at now. We like to think it's perfect, but we're sure you have some suggestions. Please, let us know what they are!

Feedback
Your Email Address
Because we'd like to talk with you!

Support Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.