I'm using this script to alow members to login:
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
// Get the user's input from the form
$name = $_POST['username'];
$pass = $_POST['password'];
// Register session key with the value
$_SESSION['username'] = $name;
// Set variables
$try_again = "Incorrect \"username\" or \"password\"";
$members_area = "members_area.php";
addslashes($name);
include "db.php";
$sql = "SELECT DISTINCT * FROM `users` WHERE `user` = '$name'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
$_SESSION['username'] = FALSE;
echo "<h4 align=center>$try_again</h4><br>
<div align=center><a href='login.php'>Try Again</a></div>";
exit;
}
while ($row = mysql_fetch_array($result)){
extract($row);
$_SESSION['artist'] = $artist;
if (!isset($_POST['submit']))
{
echo "There is a problem please contact the webmaster and quote this ref number (1)";
}
//check if username and passwords are correct
else {
if ($name == $user && $pass == $PASSWORD)
{
echo "<div align=center>Welcome $user <br><a href='members_area.php'>Enter</a></div>";
}
//wrong username and password
else {
$_SESSION['username'] = FALSE;
echo "<h4 align=center>$try_again</h4><br>
<div align=center><a href='login.php'>Try Again</a></div>";
}
}
}
mysql_free_result($result);
?>
If correct username and password are inputed it displays a link to members.php. I have created two session variables for use in members.php.
Members.php:
<?
session_start();
header("Cache-control: private"); //IE 6 Fix
$useranme = $_SESSION['username'];
$artist = $_SESSION['artist']; ?>
<table width="100%" border="0" cellpadding="5">
<tr>
<td width="23%"> </td>
<td width="46%"> </td>
<td width="31%"><div align="right">Welcome <? echo $_SESSION['username']; ?> </div></td>
</tr>
<tr>
<td height="29"> </td>
<td> </td>
<td><div align="right"><a href="logout.php">Log-out</a></div></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="5">
<tr bgcolor="#F2F2F2">
<td width="46%">Title
<?
include "db.php";
$sql = "SELECT * FROM `cds` WHERE `artist` = '$artist'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "<h4 align=center>There is a problem please contact the webmaster and quote this ref number (1)</h4>";
exit;
}
?>
</td>
<td width="8%"><div align="left">Status</div></td>
<td width="16%">Promotion</td>
<td width="8%">Price</td>
<td width="7%"> </td>
<td width="15%"> </td>
</tr>
<? while ($row = mysql_fetch_array($result))
{
extract($row);
if ($cd_title == NULL) { echo "No CD's Available";
}
else {
echo "<tr><td>".$cd_title."</td>";
echo "<td width='10%'>".$cd_status."</td>";
echo "<td width='11%'>".$cd_promotion."</td>";
echo "<td width='6%'>".$cd_price."</td>";
echo "<td width='7%'><div align='right'>";
echo "<form action='edit_cd.php' method='POST'>";
echo "<input type='hidden' name='cd_title' value='".$cd_title."'>";
echo "<input name='POST' type='submit' value='Edit'>";
echo "</form></div></td>";
echo "<td width='13%'><div align='right'>View Sales Stats</div></td></tr>";
}
}
?>
</table>
I'm getting these error's:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/junglej/public_html/artists/members_area.php:1) in /home/junglej/public_html/artists/members_area.php on line 2
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/junglej/public_html/artists/members_area.php:1) in /home/junglej/public_html/artists/members_area.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at /home/junglej/public_html/artists/members_area.php:1) in /home/junglej/public_html/artists/members_area.php on line 3
I do need to use session_start(); at the top of each page dont I?
How can the header info already been have been sent when we on a new page (members.php)?