dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: coooooooookies!

  1. #1
    Member
    Join Date
    Oct 2007
    Posts
    81

    coooooooookies!

    Hi guys, im trying to create a voting system where you can vote for games and stuff.. Your vote is stored on my DB, but you will also receive a cookie so you cant revote..
    PHP Code:
    <?

    if(isset($_GET["v"])) {
    $myvote $_GET["v"];
    $cookiename $_GET["slug"];
    $gameid $_GET["id"];
    $newvalue $_COOKIE["vgratgam"].",".$cookiename;
    $mycookie $_COOKIE["vgratgam"];
    $cookiearray explode(","$mycookie);


    if (isset(
    $_COOKIE["vgratgam"])) { 
    //Cookie exist, check if you already voted for this game.

    foreach ($cookiearray as $regvot) {
    if(
    $regvot == $cookiename) {
    $timesvoted $timesvoted 1;
    } else { 
    $timesvoted $timesvoted 0;
    }

    if(
    $timesvoted == '0') {
    //user already voted on this submission!
    header("Location: ../play.php?id=$gameid&e");
    } else {
    setcookie(vgratgam$newvaluetime()+86400); // Update the cookie!
    header("Location: ../play.php?id=$gameid&s");


    require_once(
    "DbConnector.php"); 
    $db = new DbConnector();
    $db->connect();

    $query "SELECT * FROM games WHERE id='$gameid'";
    $result $db->query($query);
    $rows $db->fetchArray($result); // Get the profile from database

    $votes $rows['votes'];
    $score $rows['score'];

    $votes $votes 1;
    $score $score $myvote;
    $totscore $score $votes;


    mysql_query("UPDATE games SET score='$score', votes='$votes', totalscore='$totscore' WHERE id='$gameid'");
    } }
    } else { 
    // the cookie expired or it was never created 
    header("Location: ../play.php?id=$gameid&s");
    setcookie(vgratgam$cookienametime()+86400); // so make one 

    require_once("DbConnector.php"); 
    $db = new DbConnector();
    $db->connect();

    $query "SELECT * FROM games WHERE id='$gameid'";
    $result $db->query($query);
    $rows $db->fetchArray($result); // Get the profile from database

    $votes $rows['votes'];
    $score $rows['score'];

    $votes $votes 1;
    $score $score $myvote;
    $totscore $score $votes;


    mysql_query("UPDATE games SET score='$score', votes='$votes', totalscore='$totscore' WHERE id='$gameid'");
    }



    }
    ?>
    So what this does, is saving your vote to a DB, and saves a cookie, I want to get ALL the games you vote on in ONE cookie, not thousands of them.

    Then if you dont have that cookie it creates a new one, if you have it, it uses explode function to see if you voted at this particular game. And if you havent, it saves your vote and updates it.


    What I get so far, is that I always get sucsessfull vote, althrough the cookie gets updated, just that the same game gets listed a couple of times:S

    What am I doing wrong:S?

  2. #2
    Senior Member
    Join Date
    Mar 2000
    Posts
    584
    Not exactly sure, but one probable thing is that you are not exiting after your header calls.

    Once you do header(), the rest of the code still executes (and cookies get set etc).

    Also, you might want to specify a path in your cookie setting.
    InFlash.com shows, games, greetings and more - you in?

  3. #3
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    I would recommend using sharedObjects (flash cookies) instead as they are more persistent and wont get deleted when someone deletes their browser cookies.. (however they still can be deleted if the end user knows where they are stored)

    heres a tutorial on sharedobjects:http://www.permadi.com/tutorial/flas...ect/index.html

    obviously the best solution would be a registration/login system .. and updating a field against that user once they have voted...
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center