Jump to content

Prevent user from voting twice with cookie


bob0011

Recommended Posts

Hi there, im hoping someone can help me with this. What i’m trying to do is prevent users from voting twice with cookies, ive tried to set several myself but none of them have worked. Any help would be amazing. Thanks.

EDIT: unsure why its formatted this way.  how to fix? 

 

<?php include 'functions.php'; // Connect to MySQL $pdo = pdo_connect_mysql(); // If the GET request "id" exists (poll id)... if (isset($_GET['id'])) { // MySQL query that selects the poll records by the GET request "id" $stmt = $pdo->prepare('SELECT * FROM polls WHERE id = ?'); $stmt->execute([$_GET['id']]); // Fetch the record $poll = $stmt->fetch(PDO::FETCH_ASSOC); // Check if the poll record exists with the id specified if ($poll) { // MySQL query that selects all the poll answers $stmt = $pdo->prepare('SELECT * FROM poll_answers WHERE poll_id = ?'); $stmt->execute([$_GET['id']]); // Fetch all the poll anwsers $poll_answers = $stmt->fetchAll(PDO::FETCH_ASSOC); // If the user clicked the "Vote" button... if (isset($_POST['poll_answer'])) { // Update and increase the vote for the answer the user voted for $stmt = $pdo->prepare('UPDATE poll_answers SET votes = votes +1 WHERE id = ?'); $stmt->execute([$_POST['poll_answer']]); // Redirect user to the result page header ('Location: result.php?id=' . $_GET['id']); exit; } } else { die ('Poll with that ID does not exist.'); } } else { die ('No poll ID specified.'); } ?> <?=template_header('Poll Vote')?> <div class="content poll-vote"> <h2><?=$poll['title']?></h2> <p><?=$poll['des']?></p> <form action="vote.php?id=<?=$_GET['id']?>" onSubmit="disable()" method="post"> <?php for ($i = 0; $i < count($poll_answers); $i++): ?> <label> <input type="radio" name="poll_answer" value="<?=$poll_answers[$i]['id']?>"<?=$i == 0 ? ' checked' : ''?>> <?=$poll_answers[$i]['title']?> </label> <?php endfor; ?> <div> <input type="submit" name="submit" value="Vote"> <a href="result.php?id=<?=$poll['id']?>">View Result</a> </div> </form> </div> <?=template_footer()?>

 

Edited by requinix
cleaning up post
Link to comment
Share on other sites

47 minutes ago, bob0011 said:

is there a way to fix it? 

Fixed. What editor are you using?

<?php 
include 'functions.php'; 
// Connect to MySQL 
$pdo = pdo_connect_mysql(); 
// If the GET request "id" exists (poll id)... 
if (isset($_GET['id'])) { 
    // MySQL query that selects the poll records by the GET request "id" 
    $stmt = $pdo->prepare('SELECT * FROM polls WHERE id = ?'); 
    $stmt->execute([$_GET['id']]); 
    // Fetch the record 
    $poll = $stmt->fetch(PDO::FETCH_ASSOC); 
    // Check if the poll record exists with the id specified 
    if ($poll) { 
        // MySQL query that selects all the poll answers 
        $stmt = $pdo->prepare('SELECT * FROM poll_answers WHERE poll_id = ?'); 
        $stmt->execute([$_GET['id']]); 
        // Fetch all the poll anwsers 
        $poll_answers = $stmt->fetchAll(PDO::FETCH_ASSOC); 
        // If the user clicked the "Vote" button... 
        if (isset($_POST['poll_answer'])) { 
            // Update and increase the vote for the answer the user voted for 
            $stmt = $pdo->prepare('UPDATE poll_answers SET votes = votes +1 WHERE id = ?'); 
            $stmt->execute([$_POST['poll_answer']]); 
            // Redirect user to the result page 
            header ('Location: result.php?id=' . $_GET['id']); 
            exit; 
        } 
    } 
    else { 
        die ('Poll with that ID does not exist.');
    }
} 
else { 
    die ('No poll ID specified.'); 
} 
?> 
<?=template_header('Poll Vote')?> 
<div class="content poll-vote"> 
    <h2><?=$poll['title']?></h2>
    <p><?=$poll['des']?></p> 
    <form action="vote.php?id=<?=$_GET['id']?>" onSubmit="disable()" method="post"> 
        <?php 
        for ($i = 0; $i < count($poll_answers); $i++): 
        ?> 
            <label> 
                <input type="radio" name="poll_answer" value="<?=$poll_answers[$i]['id']?>" <?=$i == 0 ? ' checked' : ''?>> 
                <?=$poll_answers[$i]['title']?> 
            </label> 
        <?php endfor; ?> 
        <div> 
            <input type="submit" name="submit" value="Vote"> 
            <a href="result.php?id=<?=$poll['id']?>">View Result</a> 
        </div> 
    </form> 
</div> 
 <?=template_footer()?>

 

Link to comment
Share on other sites

6 minutes ago, Barand said:

Fixed. What editor are you using?


<?php 
include 'functions.php'; 
// Connect to MySQL 
$pdo = pdo_connect_mysql(); 
// If the GET request "id" exists (poll id)... 
if (isset($_GET['id'])) { 
    // MySQL query that selects the poll records by the GET request "id" 
    $stmt = $pdo->prepare('SELECT * FROM polls WHERE id = ?'); 
    $stmt->execute([$_GET['id']]); 
    // Fetch the record 
    $poll = $stmt->fetch(PDO::FETCH_ASSOC); 
    // Check if the poll record exists with the id specified 
    if ($poll) { 
        // MySQL query that selects all the poll answers 
        $stmt = $pdo->prepare('SELECT * FROM poll_answers WHERE poll_id = ?'); 
        $stmt->execute([$_GET['id']]); 
        // Fetch all the poll anwsers 
        $poll_answers = $stmt->fetchAll(PDO::FETCH_ASSOC); 
        // If the user clicked the "Vote" button... 
        if (isset($_POST['poll_answer'])) { 
            // Update and increase the vote for the answer the user voted for 
            $stmt = $pdo->prepare('UPDATE poll_answers SET votes = votes +1 WHERE id = ?'); 
            $stmt->execute([$_POST['poll_answer']]); 
            // Redirect user to the result page 
            header ('Location: result.php?id=' . $_GET['id']); 
            exit; 
        } 
    } 
    else { 
        die ('Poll with that ID does not exist.');
    }
} 
else { 
    die ('No poll ID specified.'); 
} 
?> 
<?=template_header('Poll Vote')?> 
<div class="content poll-vote"> 
    <h2><?=$poll['title']?></h2>
    <p><?=$poll['des']?></p> 
    <form action="vote.php?id=<?=$_GET['id']?>" onSubmit="disable()" method="post"> 
        <?php 
        for ($i = 0; $i < count($poll_answers); $i++): 
        ?> 
            <label> 
                <input type="radio" name="poll_answer" value="<?=$poll_answers[$i]['id']?>" <?=$i == 0 ? ' checked' : ''?>> 
                <?=$poll_answers[$i]['title']?> 
            </label> 
        <?php endfor; ?> 
        <div> 
            <input type="submit" name="submit" value="Vote"> 
            <a href="result.php?id=<?=$poll['id']?>">View Result</a> 
        </div> 
    </form> 
</div> 
 <?=template_footer()?>

 

Hi there, i am using sublime text 3 

Link to comment
Share on other sites

Set a cookie or local storage key when someone votes; check for it before you process a vote. Admittedly, if your poll is available for a long time this won't be greatly effective, but if you poll is only up for a day or so it could work.

Edited by maxxd
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.