I am trynig to built a poll system. i want to place it in a div of a php file. So i created a form that takes all the data that needs from 2 txt files (questions, options etc). When the user votes the vote.php executes.



$poll_file = "poll.txt";
$options_file = "options.txt";

   if (isset($_SESSION['SESS_LAST_NAME']))
           $redirect_page = "../../blog/blog.php";
     $redirect_page = "../../blog/blog1.php";
     // The page the user will be redirect to after voting

    $display_votes = false; // If you want to leave the vote totals for each option hidden until the user votes, leave this at false; if you want the vote totals for each option to be shown all the time, change it to true

    $read_options = fopen($options_file, "r");
    $options = fread($read_options, 1024);
    $options = explode("\n", $options);

    $read_poll = fopen($poll_file, "r");
    $choices = fread($read_poll, 7024);
    $choices = explode("|", $choices);

    $date = $options[0];
    $header = $options[1];
    $question = $options[2];
    $cookie_name = $options[3];
    //echo "cookie name=".$cookie_name;
    //$redirect_page = $options[4];
    $cookie_timeout = $options[5];
    $total_votes = $choices[10]+$choices[11]+$choices[12]+$choices[13]+$choices[14]+$choices[15]+$choices[16]+$choices[17]+$choices[18]+$choices[19];
    $choice = $_POST["choice"];

    if(isset($choice) && !isset($_COOKIE["$cookie_name"])) {

    setcookie("$cookie_name", "true", time()+$cookie_timeout);

    $pick = "1".$choice;
    $choices[$pick] += 1;

    $file_write = $choices[0]."|".$choices[1]."|".$choices[2]."|".$choices[3]."|".$choices[4]."|".$choices[5]."|".$choices[6]."|".$choices[7]."|".$choices[8]."|".$choices[9]."|".$choices[10]."|".$choices[11]."|".$choices[12]."|".$choices[13]."|".$choices[14]."|".$choices[15]."|".$choices[16]."|".$choices[17]."|".$choices[18]."|".$choices[19];

    $choices_file = fopen($poll_file, "w");
    fwrite($choices_file, $file_write);

    header("Location: $redirect_page?message=1#poll");

    elseif(!isset($choice)) {
    header("Location: $redirect_page?message=2#poll");

    elseif(isset($_COOKIE["$cookie_name"])) {
    header("Location: $redirect_page?message=3#poll");

it seems that the cookie never sets. I can not understand why. Is there anyone that can help?

Recommended Answers

All 2 Replies

put session_start() at the top of your script

thanks, i just forgot to copy the session start. i found the problem. the $cookie_timeout was empty and the cookie was creating and dying at the same time. Thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.