Cookie Header Wrning???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Cookie Header Wrning???

Post 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!
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

What's with the smillies above :lol:
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

I had a look at it. But isn't session_start(); before everything else?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Thanks! I'll test it!
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Seems to have worked! No errors! Thanks!!!!!!!!!!!!!!!!!!!!!!!!
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

all part of a days work :)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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")
Post Reply