-1

我想为从数据库中检索到的几篇文章建立向上/向下投票系统,但我想为每篇文章添加 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 为空,我将以下函数编码为 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);
    }
4

1 回答 1

0

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

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

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

此外,通过数据库中的投票表,您可以轻松捕获对所有内容投赞成票或反对票的机器人,并限制单个 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/来检查用户是否有任何以前上传的文件(尝试上传一些东西并关闭浏览器并再次去那里,它将在列表中)。

于 2016-07-27T20:15:19.953 回答