Page 1 of 1

Login Cookies and Session Problems...

Posted: Wed Oct 13, 2004 5:33 am
by Dale
Heres my login.php page:

Code: Select all

<?php
	require "header.php";
?>

<tr>
<td width="100%" bgcolor="<?php print $the_contentbgcolor; ?>">
<?php
echo "<font face="$the_font" size="$the_size1"><a href="./index.php">$set_title</a> --> $lang_login</font>";
?><center><br>

<form action="loginl.php" method="POST">
<table border="0" width="<?php print $the_contentwidth; ?>" cellspacing="<?php print $the_bordersize; ?>" cellpadding="<?php print $the_cellspacing; ?>" bgcolor="<?php print $the_bordercolor; ?>">

<?php
echo"<tr>
<td width="100%" background="./images/$the_bckgrndcat" bgcolor="$the_topcolor" align="left" colspan="2"><font face="$the_font" size="$the_size1" color="$the_toptextcolor"><b>$lang_login</b></font></td>
</tr>
<tr>
<td width="50%" bgcolor="$the_altcolor1" align="left"><font face="$the_font" size="$the_size2"><b>$lang_username</b></font></td>
<td width="50%" bgcolor="$the_altcolor2" align="left"><input type="text" name="username"></td>
</tr>
<tr>
<td width="50%" bgcolor="$the_altcolor1" align="left"><font face="$the_font" size="$the_size2"><b>$lang_password</b></font></td>
<td width="50%" bgcolor="$the_altcolor2" align="left"><input type="password" name="password"></td>
</tr>
<tr>
<td width="100%" bgcolor="$the_altcolor1" align="center" colspan="2"><input type="submit" value="Log Into Account"></td>
</tr>";
?>

</table>
</form>

<br>
</center></td>
</tr>
</table>
<?php
	require "footer.php";
?>
Now heres where it goes (loginl.php):

Code: Select all

<?php
	require "header.php";
?>

<tr>
<td width="100%" bgcolor="<?php print $the_contentbgcolor; ?>">
<center><br>
<table border="0" width="<?php print $the_contentwidth; ?>" cellspacing="<?php print $the_bordersize; ?>" cellpadding="<?php print $the_cellspacing; ?>" bgcolor="<?php print $the_bordercolor; ?>">
<?php
if ((!$_POST[username]) || (!$_POST[password])) {
echo"<tr>
<td width="100%" background="./images/$the_bckgrndcat" bgcolor="$the_topcolor" align="left"><font face="$the_font" size="$the_size1" color="$the_toptextcolor"><b>$lang_login</b></font></td>
</tr>
<tr valign="top">
<td width="100%" bgcolor="$the_altcolor1" align="left"><font face="$the_font" size="$the_size2">$lang_login_forms</font></td>
</tr>";
exit;
}

$sql = "SELECT username, password FROM df_users WHERE username = '$_POST[username]' AND password = password('$_POST[password]')";
$result = mysql_query($sql,$conn) or die(mysql_error());

if (mysql_num_rows($result) == 1) {

$username = mysql_result($result, 0, 'username');

setcookie("dforum", "1", time()+3600, "/", "/", 0);

session_start();

$_SESSION[username] = "$username";

echo "<tr>
<td width="100%" background="./images/$the_bckgrndcat" bgcolor="$the_topcolor" align="left"><font face="$the_font" size="$the_size1" color="$the_toptextcolor"><b>$lang_login</b></font></td>
</tr>
<tr valign="top">
<td width="100%" bgcolor="$the_altcolor1" align="left"><font face="$the_font" size="$the_size2">$lang_login_yes</font></td>
</tr>";
} else {
echo "<tr>
<td width="100%" background="./images/$the_bckgrndcat" bgcolor="$the_topcolor" align="left"><font face="$the_font" size="$the_size1" color="$the_toptextcolor"><b>$lang_login</b></font></td>
</tr>
<tr valign="top">
<td width="100%" bgcolor="$the_altcolor1" align="left"><font face="$the_font" size="$the_size2">$lang_login_no</font></td>
</tr>";
}
?>

</table>
<br>

</center></td>
</tr>
</table>
<?php
	require "footer.php";
?>
But for some reason, when i log in successfully I get the following errors:

Code: Select all

Warning: Cannot add header information - headers already sent by (output started at c:\program files\appserv\www\forums\header.php:8) in c:\program files\appserv\www\forums\loginl.php on line 27

Warning: Cannot send session cookie - headers already sent by (output started at c:\program files\appserv\www\forums\header.php:8) in c:\program files\appserv\www\forums\loginl.php on line 29

Warning: Cannot send session cache limiter - headers already sent (output started at c:\program files\appserv\www\forums\header.php:8) in c:\program files\appserv\www\forums\loginl.php on line 29

Posted: Wed Oct 13, 2004 5:48 am
by kettle_drum
You cannot send ANY data to the user before you set a header/cookie. Make sure your not sending any html code - or even a white space - before you set the cookie.

Posted: Wed Oct 13, 2004 6:03 am
by Dale
What... so this has to be in the very top of the code?

Code: Select all

setcookie("dforum", "1", time()+3600, "/", "/", 0);
session_start();
$_SESSION[username] = "$username";

Posted: Wed Oct 13, 2004 6:13 am
by kettle_drum
Yes, or you can use an output buffer with ob_start() and ob_flush();

Posted: Wed Oct 13, 2004 6:29 am
by Dale
I've never used them, could you please show me an example please...

Posted: Wed Oct 13, 2004 6:37 am
by twigletmac
The easiest solution is to make sure that setcookie() and session_start() are at the very top of the page. Output buffering is not really neccessary in most cases.

BTW, read the second link in my sig as to why you need to make some changes to the way you're using arrays.

Mac

Posted: Thu Oct 14, 2004 3:15 am
by Dale
Ah! Thanks it works fine now ;)