View Full Version : headers already sent error.

05-14-2008, 08:54 AM
hi i am getting the following error when i log onto a php page i have made:
Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\(removed)\Desktop\xampplite\htdocs\LoginForm.php:14) in C:\Documents and Settings\(removed)\Desktop\xampplite\htdocs\LoginForm.php on line 76

code from LoginForm.php:


<title>.:Login Page:.</title>


<h1>Welcome to the login page!</h1>

<!--the server [php_self] variable here allows us to post
form data back to the same page-->

<form action="<?=$_SERVER['PHP_SELF']?>"method="post">

<p>Login: <input type="text" name="LoginName" size="30" maxlength="30"/></p>
<p>Password: <input type="password" name="Pass" size="8" maxlength="8" /> </p>
<p><input type="submit" name="submit" value="submit" /></p>


//check for the submit variable and if it exists process the form

//set $_POST variables here
$username = $_POST[LoginName];
$pass = $_POST[Pass];
echo 'Welcome';

//connect to mysql
$con = mysql_connect("localhost","root");

//if the connection doesn't work then die with message
if (!$con)
die('could not connect;' . mysql_error());

//select the database or die with message
or die('could not find database' . mysql_error());

//create the query
$query1 = "SELECT UserId FROM logininfo WHERE (LoginName='$username' AND Pass='$pass')";

//execute the query or die with message
$result = mysql_query($query1)
or die ("Error in query" . mysql_error());

//set the variable $num to equal the number or rows returned
$num = mysql_num_rows($result);
if($username == NULL || $pass == NULL)
echo 'you need to fill in all fields';
elseif($num !=1 && $username !=NULL && $pass != NULL) {
echo 'sorry you have the wrong login / password combination<br />';
echo 'try again or...<br />';
echo 'if you are not registered please go <a href="register.php">HERE</a>';

05-14-2008, 09:04 AM
Just google the error, you'll get plenty of results, http://www.google.co.in/search?hl=en&q=Cannot+modify+header+information+-+headers+already+sent

(Instead of echoing those errors, just store them in an array and then echo them at the end)

05-14-2008, 09:17 AM
Your problem should be here:


Is this page being loaded by itself or being included in another page? Try another form of redirection and it will work.

05-14-2008, 03:34 PM
Simple solution, put the php code that comes after your </html> tag into a seperate file, e.g(inc_login.php). Remove the echo 'Welcome'; line. And instead of echoing out errors, store them in variables and then echo these variables on the html page.

Include this file at the top of your page before the <html> tag.


<?php require_once('inc_login.php'); ?>

Voilla everything now works.

This is because you cannot alter header information once output has already started. By including the php at the begining of the page before the <html> tag header information can now be altered.

On a side note it's good practice to seperate your php code from your html, it makes for much easier editing later on. I reccomend setting up a folder called 'includes' or something along those lines that has all your php files in it. Then in your html pages include the required php files from that folder.