在 up/down 投票系统中使用 cookie

using cookies in up/down voting system

我想为从数据库中检索到的几篇文章构建Up/down投票系统,但我想为每篇文章添加 cookie 以限制投票数,这样 cookie 将在一天后过期,但我没有知道在哪里添加适当的代码。

更多详情:

<script>
    function vote(id, value) { // function vote with 2 arguments: article ID and value (+1 or -1) depending if you clicked on the arrow up or down
        var dataFields = { 'id': id, 'value': value }; // We pass the 2 arguments
        $.ajax({ // Ajax
            type: "POST",
            dataType: "text",//This for indicate that you'r expecting a text response from server
            url: "WebService.asmx/updateVotes",
            data: dataFields,
            timeout: 3000,

            success: function (dataBack) {
                if(
                    $('#number' + id).html(dataBack);// div "number" with the new number
                    $('#arrow_up' + id).html('<div class="arrow_up_voted"></div>'); // We replace the clickable "arrow up" by the not clickable one
                    $('#arrow_down' + id).html('<div class="arrow_down_voted"></div>'); // We replace the clickable "arrow down" by the not clickable one
                    $('#message' + id).html('<div id="alertFadeOut' + id + '" style="color: green">Thank you for voting</div>'); // Diplay message with a fadeout
                    $('#alertFadeOut' + id).fadeOut(1100, function () {
                        $('#alertFadeOut' + id).text('');
                    });
                },


            error: function () {
                $('#number' + id).text('Problem!');
            }
        });
    }


</script>

以上代码是调用ajax方法的脚本,每次用户点击向上箭头时增加每人的投票数,反之减少。

    public string updateVotes(string id,int value)
{

    System.Threading.Thread.Sleep(500); // delay for 2.5 seconds Network latency

   post p = db.posts.Find(int.Parse(id));
    // assign new values
   p.totalVotes += value;
    db.Entry(p).State = System.Data.EntityState.Modified;
    db.SaveChanges();

    string dataBack =p.totalVotes.ToString();
    return dataBack;
}

这是网络方法。

现在我试着大声思考,如果 cookie 是否为 null,我将以下函数编码到 ewxamine。

 public bool enableVoting()
{

    HttpCookie cookie = Request.Cookies["enableVote"];

    if (Request.Cookies["enableVote"] != null)
    {
       return true;

    }

    else
    {

      return false;
    }


}

我知道这是错误的,但至少我试过了。

另外,当用户为文章投票时,在哪里添加 for each 循环以添加 cookie。?

   foreach(post p in db.posts){
        HttpCookie cookie = new HttpCookie("enableVote"+p.ID);
        cookie.Value = "article:"+p.ID;
        cookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(cookie);
    }

一个建议。不要为此使用 cookie。它们很容易被操纵。清除浏览器历史记录,您可以再次投票……一次又一次……又一次。

相反,在您的数据库中创建一个投票 table 并添加一条记录,其中包含选民的 ip 和他们投票的 post 的 ID 以及时间戳。

通过这种方式,您可以轻松计算票数,当有人投票时,您可以快速检查该 IP 最后一次投票给该文章(或任何文章)的时间。

此外,在您的数据库中进行投票 table,您可以轻松捕获对所有内容投赞成票或反对票的机器人,并限制单个 ip 对任何文章的投票数(不超过 1 或 2可能每隔几分钟投票一次)。

如果您担心同一 IP 背后的多个人无法投票,您可以包括浏览器名称,并且只将唯一 IP 和浏览器版本计为一票。这是相当独特的。它也可以被操纵,但对于普通用户来说有点困难。

更新: 我使用这段代码是为了在我的一个 MVC 项目中获得一个有点独特的密钥。用户可以切换浏览器并再次投票,但这比清除浏览器历史记录更费力,更痛苦。

我将 IP、浏览器和国家组合成一个字符串,并将其用作投票键。

public class UserInfo
{
    public String ip { get; private set; }
    public String browser { get; private set; }
    public String country { get; private set; }

    public UserInfo(HttpRequestBase Request)
    {
        ip = Request.UserHostAddress;
        browser = Request.Browser.Platform + " " + Request.Browser.Type + "/" + Request.Browser.Id + " " + Request.Browser.Version;
        country = "";

        if (Request.UserLanguages.Length > 0)
            country += " - " + Request.UserLanguages.ElementAt(0);
    }
}

我在这里使用这个系统:http://filepublicator.com/ 来检查用户是否有任何以前上传的文件(尝试上传一些东西并关闭浏览器并再次访问那里,它会在列表中)。