Hi,
I made a simple file based rating system using cookies for my friend's site (Sylem check out the articles) and been having a problem of showing the new rating after a user has submitted a rating. Instead of displaying the new average rating it after the user has submitted the rating, the page has to be refresh to load the new average rating.
The cookies are originally made in the articles index page. The ratings are stored in a text file, one per line. Each time the page is visited the script checks the values of the cookie to determine whether to display a rating or the submit form.
If the user submits a rating a value is sent in post to which causes the value to be added to the file depending upon the value of the cookie. The cookie value is then change to say user has voted (unless it is already).
The rating is calulated further down the page after the headers have been sent. By reading each line of the text file into a array and working out the average. However the old value is display and only on refresh is the new rating display.
I am at a lost as to why this happens (i am aware there are workarounds but i think this should work). The whole system works fine apart from this refresh. Any ideas?
Header code
<?php
ob_start();
$cookie_name= "100reasons";
$cookie_value= "no";
$cookie_life= 125798400; //the length of time the cookie exists , 125798400s =1 year
$file = "100reasons.rat"; //filename
$width= 57;
$height= 75;
$voted= false;
if (isset($_COOKIE[$cookie_name])) //cookie exists
{
$voted= ($_COOKIE[$cookie_name]); //set value of voted
}
else //if doesn't exist
{
$voted= false;
}
if (isset($_POST['view']))
{
setcookie($cookie_name, "yes", time()+$cookie_life, "/"); //change value of cookie to voted
$voted="yes"; //update value of voted
}
if (($voted=="no") and (isset($_POST['rating']))) //if voted=false and voted posted
{
if (file_exists($file))
{
if ($filehandle = fopen($file, "a+")) //open file
{
fwrite($filehandle,"\r\n".$_POST['rating'][0]); //write rating to file
setcookie($cookie_name, "yes", time()+$cookie_life, "/"); //change value of cookie to voted
$voted="yes"; //update value of voted
}
}
else
{
if ($filehandle = fopen($file, "w+")) //open file
{
fwrite($filehandle,$_POST['rating'][0]); //write rating to file
setcookie($cookie_name, "yes", time()+$cookie_life, "/"); //change value of cookie to voted
$voted="yes"; //update value of voted
}
}
}
ob_end_flush();
?>
Body code
<?php
//rating dialog
if ($voted=="no") //if cookie created and hasn't voted
{ ?>
How Bling is this page?
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="rating">
<option value="5" selected>5 - Excellent!</option>
<option value="4">4</option>
<option value="3">3 - Fair</option>
<option value="2">2</option>
<option value="1">1 - Poor</option>
<option value="0">0 - Awful!</option>
</select>
<input type="submit" value="GO" />
</form>
<?php
}
else
{
$rating=0;
$num=0;
if (file_exists($file))
{
$content = file($file);
foreach ($content as $i)
{
$rating=$rating + $i[0];
$num=$num + 1;
}
if ($num<>0)
{
$rating= $rating / $num;
echo 'Blingometre rating: '. round($rating, 2);
$i2 = round($rating, 0);
for ($i = 1; $i <= $i2; $i++)
{
echo "<img src=\"/bling.gif\" width=\"$width\" height=\"$height\">";
//echo "<img src="/images/stars/0o5.gif" width="70" height="18">";
}
echo "<p>This page has been rated $num times";
}
else
{
echo "<p>This page hasn't been rated";
}
}
else
{
echo "<p>This page hasn't been rated";
}
}
If ($voted==false)
{
echo "<p>To rate this page enable cookies";
}
?>