避免重复投票

时间:2013-04-14 01:12:18

标签: php mysql sql

我是PHP / MySQL和整个网站设计的新手。我正在建立一个预定义用户可以投票的网站。我有一个包含用户列表的数据库。我试图避免重复投票。我读到你可以阻止IP地址或使用cookie,但我试图使用另一种方法。

在我的名为'users'的数据库中,我有三列 - Username,Password和flag。 标志的默认值为0.一旦用户投票,我将该特定用户的标志设置为1.现在,如果用户再次尝试投票,我想检查数据库中的标志值。如果它是0,我会将他发送给“谢谢你投票”页面并更新我创建的另一个名为results的数据库,该数据库记录每个候选人收到的投票数。如果没有,我会带他到另一页说“你已经投了票”。到目前为止一切正常,除了我不知道如何在我的数据库中读取flag的值并使用if条件。

这是我到目前为止所拥有的:

<?php

$host="localhost"; // Host name 
$username="dbxxxxx"; // Mysql username 
$password="password"; // Mysql password 
$db_name="dbxxxxx_users"; // Database name 
$tbl_name="users"; // Table name 


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$user = $_COOKIE["details"];  //cookie details has the username the user used to log in

$SQL = "SELECT flag FROM users WHERE Username='$user'";
$flag = mysql_query( $SQL );   //no clue what's happening here. Just trying random stuff
$db_field = mysql_fetch_assoc($flag);  


if($db_field==0)     //checking the value of flag in the database
{       
    mysql_query("UPDATE result SET Votes=Votes+1 //if flag in database = 0 
    WHERE Name='Candidate1'");  //updates result for candidate1 if the user voted for 1

    $user = $_COOKIE["details"];  //reading the cookie again. can be omitted.

    mysql_query("UPDATE users SET flag=1   //changing flag to 1 so user cannot vote again
    WHERE Username='$user'");

    header("location: http://www.lithuaniavote.com/thankyou.html");
 }

else    //flag != 1 or user has already voted
{
    header("location: http://www.lithuaniavote.com/alreadyvoted.html");
}

?>

PS:此代码将数据库中的标志从0更改为1。但是,if条件有问题。即使标志为1,我也可以投票,这表明我已经投了票,换句话说,它从未带我进入已经投票的页面。

2 个答案:

答案 0 :(得分:0)

原始代码:

$SQL = "SELECT flag FROM users WHERE Username=$user";
$flag = mysql_query( $SQL );   //no clue what's happening here. Just trying random stuff
$db_field = mysql_fetch_assoc($flag);  
if($db_field==0)     //checking the value of flag in the database

试试这个:

$SQL = "SELECT flag FROM users WHERE Username = '$user'"; // $user should be in 'quotes'
$flag = mysql_query( $SQL );  // This is the actual query to the database
$db_field = mysql_result($flag, 0);  // This is the result of the query.
if($db_field===0)  // Use 3 equals signs instead of 2 in this case (which means "exactly equal to")

答案 1 :(得分:0)

我认为你应该尝试一种更清洁(并且面向未来)的方法。让我用PDO重新构建你的问题的解决方案:

namespace Voting {
    $pdo = new \PDO("mysql:host={$host};dbname={$db_name};charset=utf8", $username, $password);

    if ($query1 = $pdo->prepare("SELECT `flag` FROM `users` WHERE `Username` = ?;", [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY])) {
        if ($query1->execute([$_COOKIE["details"]])) {
            $result = $query1->fetch(\PDO::FETCH_ASSOC);

            if (intval($result["flag"]) === 0) {
                if ($query2 = $pdo->prepare("UPDATE `users` SET `flag` = '1' WHERE `Username` = ?")) {
                    $query2->execute([$_COOKIE["details"]]);
                    $pdo = null;
                    header("Location: http://www.lithuaniavote.com/thankyou.html");
                }
            } else {
                $pdo = null;
                header("Location: http://www.lithuaniavote.com/alreadyvoted.html");
            }
        }
    }
}

警告:考虑到我没有检查$_COOKIE安全性。您必须进行某种形式的消毒,以防止注射和其他漏洞。