Question:
PHP - Warning: Cannot modify header information - headers already sent by?
O Xabarin
2008-07-12 11:15:51 UTC
I am trying to use a login script but i get that error message in the title. What does it mean? even after I changed the line where it said: header("Location: members.php"); to an echo statement, I still get that error. Nowhere in my code now there is a header statement. Why do I still get that error?

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directs you to the members page<--I changed one line of code here from the original script
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
echo "Welcome " . $_COOKIE['ID_my_site'];

}
}
}
Four answers:
anonymous
2008-07-12 12:50:03 UTC
The "header" contains information about the page such as the name, it's size, any variables sent to it from another page , the refering page (the page that has linked you to the one you are on) and also information on where to redirect a user to.



Headers are the first thing sent to the browser and can only be sent once. So if you have already displayed any text on the screen before calling the "Location" function, the script will produce an error as the time for headers will have passed.



If you want to use the headers such as "location" then you will need to send them to the browser in the first line of the script.



You can also use the ob_flush function which will do all the processing of a script before sending anything to the browser although using this can increase the load on the server.



****UPDATE****



That line that is providing an error has no semicolon at the end of the statement. It should read:



Mike S
2008-07-12 16:16:07 UTC
there is some output sent before headers.

Check your source file and make sure

that the first line begins with
no spaces allowed.
Kron
2008-07-12 18:47:36 UTC
Please check that you don not have any output in the browser before your call to header.
BAA
2008-07-12 11:40:06 UTC
its a warning message you can ignore it, its sending the header twice



you can disable it by setting display_errors = Off in your php.ini if you can manage the server or by setting it into your .htaccess by adding "php_flag display_errors off"


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...