#1
  1. Amateur Webdev'er
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    143
    Rep Power
    0

    will PHP write over the same cookie?


    I have a pretty advanced script for cookie handling which I'm really getting frustrated with. It's all for a poll. I'm trying to detect on every generation of the page if in fact my single cookie exists. Then I find if it has the array in which I always store my information. Every key & value in the array stands for the `id` the polls are stored under in the database. Pretty simple: as the poll question + results are pulled out, I set a $var to its `id` then later check the user's cookie array to see if there is a match. If so, the brief voting form isn't printed.

    Anyways, when I detect that my cookie isn't there, I set my cookie's value (not serialized or crypted or anything) to 'I was just set'.

    No matter what I do, the value always stays to 'I was just set'.

    So I guess the question is: Does PHP write over its own cookie or what?
    Do I have to remove my own cookie before I set it, everytime?

    I'm guessing this is the problem.
  2. #2
  3. No Profile Picture
    Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,650
    Rep Power
    2479
    How do you detect that your cookie is not there?
  4. #3
  5. No Profile Picture
    I AM A GOLDEN GOD
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Location
    Camarillo, California
    Posts
    5,929
    Rep Power
    1169
    We'd have to see some of the code you're talking about to make a judgement on what it's doing; I get the gist of what you're saying, but maybe there's a logic error someplace and your cookie only ever gets set with one value.

    To answer your question though, PHP will not overwrite the cookie unless you tell it to. You do not have to remove the cookie, just overwrite it with the same value string, e.g.
    PHP Code:
    if (empty($_COOKIE['POLL_INFO']))
    {
        
    $poll_values[] = 'THIS_POLL';
        
    // cookie has not been set, create
        
    setcookie('POLL_INFO'$poll_valuestime());
    }
    else
    {
        
    // cookie is set, so add to it
        // read back all the values
        
    $poll_values $_COOKIE['POLL_INFO'];
        if (!(
    in_array('THIS_POLL'$poll_values)))
        {
            
    $poll_values[] = 'THIS_POLL';
            
    setcookie('POLL_INFO'$poll_valuestime());
        }

    So you can see from the example, if your POLL_INFO cookie is already set, you want to read it back in and then check to see if it contains the current poll value. If not, add it to the array and then reset the cookie. If the cookie hasn't been set yet, create the cookie with the current poll value, in which case the next time it will check for that same value.

    HTH
    "Seriously, we're not a search engine, we're actual people." ~ ManiacDan

    BookMooch.com : Give books away. Get books you want.
  6. #4
  7. Amateur Webdev'er
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    143
    Rep Power
    0
    it's a lot, but...

    PHP Code:

    $db_result 
    mysql_query("SELECT * FROM `polls` WHERE property` = 'inuse' LIMIT 1");
    while(
    $row mysql_fetch_assoc($db_result)){
       
    $pollid $row['id']; $question $row['poll'];
       
    $options explode('-'$row['options']);
       
    $votes explode('-'$row['results']);}

    function 
    Bake($store){ @setcookie("MySitepolls""$store"time()+1209600"/"); } // lasts 2 weeks

    if(isset($_COOKIE['MySitepolls'])){
       if(
    $_COOKIE['MySitepolls'] == "I was just set"){$result 3;}
       else{
          
    $values unserialize($_COOKIE['MySitepolls']);
          list(
    $data,$chksum) = $values;
          if(
    md5($data.md5('MySalt')) == $chksum){
            
    $data unserialize($data);
            foreach(
    $data as $dat){
               if(
    $dat == $pollid){$result 2;}// cookie exists, poll already voted on
               
    else{$result 3;}}}// cookie exists, poll not voted on
       
    else{$result 1;}}}// cookie exists, bad checksum (corrupt?) (wrong type of value?) (not encrypted?)
    else{$result 4;}// cookie NONexistant

    if($result == 1){$give_form 'yes'$process_vote 'no'Bake('I was just set');}
    if(
    $result == 2){$give_form 'no'$process_vote 'no';}
    if(
    $result == 3){$give_form 'yes'$process_vote 'yes';}
    if(
    $result == 4){$give_form 'yes'$process_vote 'no'Bake('I was just set');}

    if(isset(
    $_POST['vote']) && $process_vote == 'yes'){
       
    $option_count count($options);
       for(
    $i=0$i<$option_count$i++){$numbers[] = $i;}
       if(
    is_array($_POST['candidate'])){$vote_error "One choice only please!";}
       foreach(
    $numbers as $number){if($number == $_POST['candidate']){$pass 'yes';}}
       if(
    $pass != 'yes'){$vote_error "Choose from the candidates only please!";}
       else{
          
    $give_form 'no'$i=0;
          foreach(
    $votes as $pointer=>$vote){if($pointer == $_POST['candidate']){$votes[$pointer]++;}}
          
    $votes_returning "";
          foreach(
    $votes as $vote){$votes_returning .= $votes[$i].'-'$i++;}
          
    $votes_returning substr($votes_returning0, -1); // trims off last '-'
          
    $Query "UPDATE `polls` SET `results` = '$votes_returning' WHERE `property` = 'inuse' LIMIT 1";
          if(
    mysql_query($Query)){
             
    $data[$pollid] = $pollid$data serialize($data);
             
    $chksum md5($data.md5('MySalt'));
             
    $store serialize(array($data,$cksum));
             
    Bake($store);}}}
    elseif(isset(
    $_POST['vote']) && $result == 4){$enable_cookies 'yes';}

    ///
    /// later in the page, after some irrelevant stuff

    $total_results 0;
    foreach(
    $votes as $value){$total_results += $value;}
    foreach(
    $votes as $value){$ratios[] = round((($value/$total_results)*100), 1);}

    print(
    "\n<table>\n"
         
    ."<tr><td>"$question ."</td></tr>\n");

    if(
    $total_results != 0){ $i = -1;
       foreach(
    $options as $option){$i++;
          
    $percent $ratios[$i];
          
    $width_value round($percent);
          print(
    "<tr><td>$option</td></tr>\n");
          print(
    "<tr><td>");
          print(
    "<img src=\"$graphics/pollbar.gif\" width=\"$width_value\">\n");
          print(
    "$percent%</td></tr>\n");}
    }
    else{echo 
    'No votes yet.';}

    if(
    $give_form == 'yes' && !isset($enable_cookies)){
       print(
    "<tr><td>&nbsp;</td></tr>\n<tr><td class=\"pollcell\">\n<form name=\"poll\" method=\"post\" action=\"\">\n"
            
    ."<select name=\"candidate\">\n");
       
    $value = (-1);
       foreach(
    $options as $option){$value++; print("<option value=\"$value\">$option</option>\n");}
       print(
    "</select>\n<input type=\"submit\" name=\"vote\" value=\"Vote\"">");
       if(!empty(
    $error)){print("\n<br>" . $error);}
       print("
    \n\n</form>\n</td></tr>\n");}
    else{print("
    <tr><td>$total_results votes</td></tr>\n");}

    if(isset(
    $vote_error)){print("<tr><td>$vote_error</td></tr>\n");}
    if(
    $enable_cookies == 'yes'){print("<tr><td>Enable cookies to vote!</td></tr>\n");}

    print("
    </table>\n"); 
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Malaysia
    Posts
    138
    Rep Power
    14
    is it a syntax error?
    at line 9 from the end,
    an unexpected double quote appears after \"Vote\"......
    change it to this then try again......
    PHP Code:
    print("</select>\n<input type=\"submit\" name=\"vote\" value=\"Vote\">"); 
  10. #6
  11. Amateur Webdev'er
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    143
    Rep Power
    0
    there are no errors, mind you i edited it of irrelevant form stuff

    i have no warnings /errors / anything

    just cookie trouble

IMN logo majestic logo threadwatch logo seochat tools logo