Login Cookies and Session Problems...

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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Login Cookies and Session Problems...

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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";
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Yes, or you can use an output buffer with ob_start() and ob_flush();
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

I've never used them, could you please show me an example please...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Ah! Thanks it works fine now ;)
Post Reply