Page 1 of 1

Cookie Header Wrning???

Posted: Fri Feb 28, 2003 10:05 pm
by Mr. Tech
Hi!

I have this code for a login script:

Code: Select all

<?php
// ======================================
// Logged in
// ======================================
if ($mode == "login") {

session_start();
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;

	$query = "SELECT * FROM club_members WHERE username='$username' AND password='$password' AND level='1'";

	$result = mysql_query($query) or die('error making query');
	$affected_rows = mysql_num_rows($result);

	if($affected_rows == 1) {
	$array = mysql_fetch_array($result);
	$email = $array[email];
	$_SESSION['email'] = $email;
	$_SESSION['name'] = $name;
	echo "logged in!";
	}
	else {
	session_destroy();
		print ("Your Username or password is not correct, please login agian...");
	}
}

// ======================================
// Logging in
// ======================================
else {
?>

<form action="login.php" method="post">
<input type="hidden" name="mode" value="login">
<table border="0" cellspacing="1" cellpadding="5" width="500" bgcolor="#999999">
<tr>
<td colspan="2" height="30" align="center" bgcolor="#ffffff"><b>Login</b></td>
</tr>
<tr>
<td bgcolor="#ffffff">Username:</td><td bgcolor="#ffffff"><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff">Password</td><td bgcolor="#ffffff"><input type="text" name="password" size="30"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#ffffff" align="center"><input type="submit" value="Login!"></td>
</tr>
</table>
</form>

<?php
}
?>
But it gives me an error like this:

Warning: Cannot send session cookie - headers already sent by (output started at c:\program files\easyphp\www\created\club\login.php:8) in c:\program files\easyphp\www\created\club\login.php on line 25

Warning: Cannot send session cache limiter - headers already sent (output started at c:\program files\easyphp\www\created\club\login.php:8) in c:\program files\easyphp\www\created\club\login.php on line 25



Why is that?

Thanks!

Posted: Fri Feb 28, 2003 10:06 pm
by Mr. Tech
What's with the smillies above :lol:

Posted: Fri Feb 28, 2003 10:11 pm
by evilcoder
Please take the time to read the sticky thread on these forums:

viewtopic.php?t=1157

You'll find out information on headers there.

Posted: Fri Feb 28, 2003 10:17 pm
by Mr. Tech
I had a look at it. But isn't session_start(); before everything else?

Posted: Fri Feb 28, 2003 10:29 pm
by evilcoder
it sure is, ok.. do this:

Before any other code (but within the php tags) put this:

ob_start();

then at the end of the page (still in the php tags) put:

ob_end_flush();

What this does is reads everything before sending the info, hense it allows headers to be sent from anywhere on the page.

Posted: Fri Feb 28, 2003 10:38 pm
by Mr. Tech
Thanks! I'll test it!

Posted: Fri Feb 28, 2003 10:45 pm
by Mr. Tech
Seems to have worked! No errors! Thanks!!!!!!!!!!!!!!!!!!!!!!!!

Posted: Fri Feb 28, 2003 11:15 pm
by evilcoder
all part of a days work :)

Posted: Sat Mar 01, 2003 2:17 am
by hob_goblin
Also, on any pages using sessions, you should put it at the top of the page even before the if($mode == "login")