Page 1 of 1

problem with cookie for a survey

Posted: Mon May 24, 2004 12:43 pm
by os2
hi

i do a survey in php

i would like to allow only one vote

but i don't know why, i can vote 2 times...

the survey display with this script: bd.php

Code: Select all

function sondage()
{
 echo'<div id="sondage">';
 if (!isset($_COOKIE&#1111;'sondage_cookies']))
 &#123;
   $requete = "SELECT vot_libelle,vot_novote FROM vote ORDER BY vot_novote";
   $result_vote = mysql_query ($requete);// or news_mysql_die();
   $vote = mysql_fetch_object($result_vote);
   echo'<div class="titre">';
   print("<form name="Vote" method="post" action="vote.php"> $vote->vot_libelle</div>");
   while ($vote = mysql_fetch_object($result_vote))
   &#123;
     echo'<div class="element">';
     print("<input type="radio" name="choix" value="$vote->vot_novote">$vote->vot_libelle");
     echo'</div>';
   &#125;
   print("<div class='element'><input type="submit" name="Submit2" value="Voter !"1: ></form></div>");
 &#125;
 else
 &#123;
   $requete = "SELECT vot_votants FROM vote WHERE vot_novote > 0";
   $result_vote = mysql_query ($requete);// or news_mysql_die();
   $total_vot_votants = 0;
   while ($nb_vot_votants = mysql_fetch_object($result_vote))
     $total_vot_votants = $total_vot_votants + intval($nb_vot_votants->vot_votants);
   $requete = "SELECT * FROM vote ORDER BY vot_novote";
   $result_vote = mysql_query ($requete);// or news_mysql_die();
   $vote = mysql_fetch_object($result_vote);
   echo'<div class="titre">'.$vote->vot_libelle.'</div>';
   while ($vote = mysql_fetch_object($result_vote))
   &#123;
     if ($total_vot_votants != 0)
       $pcent_vote = round((intval($vote->vot_votants) / $total_vot_votants) * 100);
     else
       $pcent_vote = 0;
     $pcent_vote_txt = strval($pcent_vote);
     print("<div class="element">$vote->vot_libelle <b> $pcent_vote_txt %</b></div>");
   &#125;

 &#125;
print("</div>");
&#125;

bd.php post the result to vote.php

vote.php

Code: Select all

<?php
  $jour = getdate();
  $numjour = intval($jour&#1111;'wday']);
  if ($numjour == 0)
    $numjour = 7;
  $numlundi = abs($numjour - 8) * 24 * 60 * 60;
  $numheure = intval(date("H")) * 60 * 60;
  $numminute = intval(date("i")) * 60;
  $numseconde = intval(date("s"));
  $expir = time() - $numseconde - $numminute - $numheure + $numlundi;
  setcookie("sondage_cookies", "1", $expir);

  include "bd.php";
  connectionbd();
  $choix = $_POST&#1111;'choix'];
  if ( isset($_POST&#1111;'choix']) )
  &#123;
   $requete = "SELECT vot_votants FROM vote WHERE vot_novote=".$choix;
   $result_vote = mysql_query ($requete);
   $vote = mysql_fetch_object($result_vote);
   $nb_votants = intval($vote->vot_votants);
   $nb_votants = $nb_votants + 1;
   $votants = strval($nb_votants);
   $requete = "UPDATE vote SET vot_votants=$votants WHERE vot_novote=".$choix;
   mysql_query($requete);
  &#125;
   $URL="http://laboiteaprog.com";
   header ("Location: $URL");

?>
you can see and test the survey problem to
http://laboiteaprog.com

any idea?

--
La boîte à prog http://laboiteaprog.com

Posted: Mon May 24, 2004 1:49 pm
by d3ad1ysp0rk
works as expected for me..

Posted: Mon May 24, 2004 1:55 pm
by os2
that strange, i don't understnand why that work for some person and for some person they need to vote 2 time to work fine...