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?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Did you find this article helpful?

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Need More Help?

Help 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.
}