Possible Duplicate:
php headers already sent error
I have attached my code which am using for a very basic login in php. It giving an error
Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\netsentries\f\includes\functions.php:1)
<?php
if (!isset($_POST['username'])) {
?>
<h3>Login</h3>
<form action="" method="post">
Username<br/>
<input name="username" type="text" /><br/>
Password<br/>
<input name="password" type="password" /><br/>
<input name="" type="submit" /><br/>
</form>
<?php
}
else
{
include('includes/config.php');
$data['username']= $_POST['username'];
$data['password']= md5($_POST['password']);
connect($db_name,$db_user,$db_pass,$db_host);
$query= "select * from user where username='" . $data['username'] . "' AND password='" . $data['password'] . "'";
$result=mysql_query($query);
//check that at least one row was returned
$rowCheck = mysql_num_rows($result);
if($rowCheck > 0)
{
while($row = mysql_fetch_array($result))
{
session_start();
session_register($data['username']);
//successful login code will go here...
//echo 'Logged in!';
}
}
else
{
echo "Failed!";
}
}
?>
Am not able to find out the problem
You need to put session_start() before anything outputs on the page.
If you were trying to use any function that modifies the header (like
header
orsession_start
), you need to make sure that nothing was outputting beforehand, even whitespace -- or else you will get that error.look for white space in that file.
in particular after the ?> at the end of the file there is often white space.
so omit the closing ?> at the end of your files.
You can't
session_start
after you've outputted any HTML at all. Move thesession_start
call up to the top, and keep a variable for what to echo later. Anything not in<?php ?>
counts, even your include file.Make sure there is absolutely nothing being output before your session call - even whitespace. (See Pekka's comment :) )
Halfway down you're having
But you don't want any output before that. And you have -in the very least- this allready sent:
PUt the session on the top :D