I'm having some session code problems when trying to set up a ratings system. I'm trying to reconfigure a tutorial I find to suit my needs and I understand all the coding except where the author gets into sessions. Here's the error message I keep getting after submitting a rating:
Warning: Cannot send session cookie - headers already sent by (output started at /path/me/public_html/CAP2/test.php:3) in /path/mepublic_html/CAP2/test.php on line 29
Warning: Cannot send session cache limiter - headers already sent (output started at /path/me/public_html/CAP2/test.php:3) in /path/me/public_html/CAP2/test.php on line 29
Thanks for your vote! The new rating is 2.60 out of 5
And here's the code that is my pain:
<?php
if ($tut_id && $rating)
{
if (session_is_registered("tut$tut_id"))
{
echo "<CENTER>Sorry! You have already voted!";
}
else
{
$get_count = mysql_query
("SELECT tut_rating, tut_num_votes FROM ratings WHERE tut_id=$tut_ID");
while(list($tut_rating, $tut_num_votes)=mysql_fetch_array($get_count))
{
$new_count = ($tut_num_votes + 1);
$tut_rating2 = ($tut_rating * $tut_num_votes);
$new_rating = (($rating + $tut_rating2) / ($new_count));
$new_rating2 = number_format($new_rating, 2, '.', '');
$update_rating = mysql_query
("UPDATE ratings SET tut_rating='$new_rating2', tut_num_votes='$new_count' WHERE tut_id=$tut_id");
$sessionvar = "tut$tut_id";
session_register($sessionvar);
echo "<DIV ALIGN=center><B>Thanks for your vote!<P>";
echo "The new rating for this link is: $new_rating2 out of 5\n";
}
}
}
else
{
$sql="SELECT * FROM ratings";
$result=mysql_query($sql,$db);
$num = mysql_num_rows($result);
$cur = 1;
while ($num >= $cur)
{
$row = mysql_fetch_array($result);
$tut_id = $row["tut_id"];
$tut = $row["tut"];
$tut_rating = $row["tut_rating"];
$tut_num_votes = $row["tut_num_votes"];
echo "<TR>";
echo "<TD>$tut_id</TD><TD>$tut</TD><TD>$tut_rating</TD><TD>$tut_num_votes</TD>";
echo "<TD><form name=rating method=post action=$PHP_SELF>
<font class=subfont>Rate this Tutorial:</font>
<select name=rating>
<option value=5.0 selected>5 - Excellent!</option>
<option value=4.0>4</option>
<option value=3.0>3 - Fair</option>
<option value=2.0>2</option>
<option value=1.0>1 - Poor</option>
<option value=0.0>0 - Awful!</option>
<input type=hidden name=cmd value=do_rating>
<input type=hidden name=tut_id value=$tut_id>
<input type=submit value=Go!>
</select>
</form></TD>";
echo "</TR>";
$cur++;
}
}
?>
The line it seems to be annoyed by is:
$sessionvar = "tut$tut_id";