General Community > Scripting Help

Session issue

(1/1)

Minion:
What is going wrong here.
i am trying to check for and set a session to allow people to vote only once.


--- Code: ---function do_rating($tut_id, $rating){
    if (session_is_registered("tut_id")){
        echo "<center>Sorry! You have already voted!";
    } else {
        $get_count = mysql_query("SELECT tut_rating, tut_num_votes FROM calendar WHERE 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 = (($_POST['rating'] + $tut_rating2) / ($new_count));
            $new_rating2 = number_format($new_rating, 2, '.', '');
            $update_rating = mysql_query("UPDATE calendar SET tut_rating='$new_rating2',tut_num_votes='$new_count' WHERE ID=$tut_id");
            $sessionvar = "$tut_id";
            session_register($sessionvar );
           
            echo '<title>Vote!</title><div align="center"><b>
            <font face="Arial"><p>Thanks for your vote!</p><p>The new rating for this show is:
            '.$new_rating2.' out of 5</font></p>';
        }

    }
    echo '<input type=button value="Close Window" onClick="javascript:self.close();">';
--- End code ---

thanks

[Unknown]:
I'd do it like this...

if (!empty($_SESSION['voted']) && $_SESSION['voted'] == $tut_id)
   // You've voted.
else
   $_SESSION['voted'] = $tut_id;

session_is_registered, etc... are deprecated and annoying functions :P.

-[Unknown]

Minion:
Tried it.
Still no dice.

I think i may have a problem placing session_start();

Basically i have 3 functions:


--- Code: ---<?php

// Rating form.

function ratemenu($tut_id){
    echo &#39;<form action="" method="post" name="rating">
    <font face="Arial" size="2" ></font>
    <select name="rating">
    <option value="5.0" selected>5 - Excellent!</option>
    <option value="4.0">4 - Good</option>
    <option value="3.0">3 - Fair</option>
    <option value="2.0">2 - Ok</option>
    <option value="1.5">1 - Poor</option>
    <option value="1.0">0 - Awful!</option></select>
    <input type="hidden" name="cmd" value="do_rating">
    <input type="hidden" name="tut_id" value="&#39;.$tut_id.&#39;">
   <input type="submit" name="Submit" value="Submit">
    </form>&#39;;
   }

// Check session match. Add rating.

function do_rating($tut_id, $rating){ 
   if (!isset($_SESSION[&#39;voted&#39;]) && $_SESSION[&#39;voted&#39;] == $tut_id){
        echo "<center>Sorry! You have already voted!";
    } else {
       $get_count = mysql_query("SELECT tut_rating, tut_num_votes FROM calendar WHERE 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 = (($_POST[&#39;rating&#39;] + $tut_rating2) / ($new_count));
            $new_rating2 = number_format($new_rating, 2, &#39;.&#39;, &#39;&#39;);
            $update_rating = mysql_query("UPDATE calendar SET tut_rating=&#39;$new_rating2&#39;,tut_num_votes=&#39;$new_count&#39; WHERE ID=$tut_id");
         session_register("voted");
         
         echo &#39;<title>Vote!</title><div align="left"><b>
            <font face="Arial" size="1">Thanks for your vote!<br />This show is now rated:
            &#39;.$new_rating2.&#39; out of 5</font>&#39;;
        }
    }
}

// Display Stars

function tut_stars($tut_rating){
    if((($tut_rating >= 0)or($tut_rating == 0)) && ($tut_rating <= 0.50)){
        echo &#39;<img src="images/stars/0o5.jpg" width="70" height="18">&#39;;
    }
    if((($tut_rating >= 0.10)or($tut_rating == 0.50)) && ($tut_rating <= .99)){
        echo &#39;<img src="images/stars/05o5.jpg" width="70" height="18">&#39;;
    }
    if((($tut_rating >= 1.00)or($tut_rating == 1.50)) && ($tut_rating <= 1.49)){
        echo &#39;<img src="images/stars/1o5.jpg" width="70" height="18">&#39;;
    }
    if((($tut_rating >= 1.50)or($tut_rating == 1.50)) && ($tut_rating <= 1.99)){
        echo &#39;<img src="images/stars/15o5.jpg" width="70" height="18">&#39;;
    }
    if((($tut_rating >= 2.00)or($tut_rating == 2.00)) && ($tut_rating <= 2.49)){
        echo &#39;<img src="images/stars/2o5.jpg" width="70" height="18">&#39;;
    }

    if((($tut_rating >= 2.50)or($tut_rating == 2.50)) && ($tut_rating <= 2.99)){
        echo &#39;<img src="images/stars/25o5.jpg" width="70" height="18">&#39;;
    }

    if((($tut_rating >= 3.00)or($tut_rating == 3.00)) && ($tut_rating <= 3.49)){
        echo &#39;<img src="images/stars/3o5.jpg" width="70" height="18">&#39;;
    }

    if((($tut_rating >= 3.50)or($tut_rating == 3.50)) && ($tut_rating <= 3.99)){
        echo &#39;<img src="images/stars/35o5.jpg" width="70" height="18">&#39;;
    }
    if((($tut_rating >= 4.00)or($tut_rating == 4.00)) && ($tut_rating <= 4.49)){
        echo &#39;<img src="images/stars/4o5.jpg" width="70" height="18">&#39;;
    }
    if((($tut_rating >= 4.50)or($tut_rating == 4.50)) && ($tut_rating <= 4.99)){
        echo &#39;<img src="images/stars/45o5.jpg" width="70" height="18">&#39;;
    }
    if($tut_rating == 5.0){
        echo &#39;<img src="images/stars/5o5.jpg" width="70" height="18">&#39;;
    }

?>

--- End code ---

they are called by if statments located in a page.
The entire rating process stays on one page.

I can't seem to get the session to stay alive maybe?

should session_start(); be at the top?

[Unknown]:
Yes... you must start the session somewhere.

-[Unknown]

Navigation

[0] Message Index

Go to full version