简体   繁体   English

如何防止upvote / downvote按钮的Autoclick Spam?

[英]How to prevent Autoclick Spam of upvote/downvote button?

I have a upvote button, which the user can press and a vote will be registered. 我有一个upvote按钮,用户可以按下该按钮并进行投票。 On clicking this button again, the vote gets cancelled. 再次单击此按钮,投票将被取消。

So each press of this button is doing a DataBase Write. 因此,每次按下此按钮都会执行数据库写入。 If an autoclicker is used on this button continuously, continuous DB calls will happen. 如果在此按钮上连续使用自动闪烁,则会发生连续的DB调用。 I want to prevent this. 我想阻止这个。 What can I do? 我能做什么?

PS. PS。 I am sending an ajax query to the backend(running Django) when the upvote button is clicked. 当点击upvote按钮时,我向后端发送ajax查询(运行Django)。

You'd really want to check this on the server, so it doesn't matter if someone disables javascript. 你真的想在服务器上检查一下,所以如果有人禁用javascript并不重要。

If you're using PHP one option is to set a time limit between votes.. eg only allow one vote per minute. 如果您正在使用PHP,则一个选项是设置投票之间的时间限制。例如,每分钟只允许一次投票。

So on every vote, store the time of the vote in a session variable, then ignore subsequent votes if it is within the time limit: 因此,在每次投票时,将投票时间存储在会话变量中,如果它在时间限制内,则忽略后续投票:

//on vote
$now=date('U');
if(!isset($_SESSION['lastvote']) || $now - $_SESSION['lastvote']<LIMIT){
    $_SESSION['lastvote']=$now;
    // do database call

}else{
    //throw error
}

Probably the easiest approach is to disable the buttons while a single operation is running. 可能最简单的方法是在单个操作运行时禁用按钮。

To obtain this, assuming you're using a JQuery ajax request to call the "upvote" / "downvote" method you simply have to: 要获得这一点,假设您使用JQuery ajax请求来调用“upvote”/“downvote”方法,您只需:

$("#upvoteButton").click(function)
{
    $("#upvoteButton").attr("disabled");
    $.ajax({
      url: 'url/upvote.extension',
      success: function(data) {
        $("#upvoteButton").removeAttr("disabled");
      }
    });
}

This way a single request is send & resolved per single tab / window. 这样,每个选项卡/窗口发送和解析单个请求。

If you trust that your users accept cookies, you could store the click together with the object id and a timestamp in the session, and test whether the user clicked the vote button during the last n seconds. 如果您信任您的用户接受cookie,您可以将点击与对象ID和时间戳一起存储在会话中,并测试用户是否在最近n秒内点击了投票按钮。

You also could disable the vote button on the web page with javascript, and enable it again after n seconds to revoke the vote. 您还可以使用javascript禁用网页上的投票按钮,并在n秒后再次启用它以撤消投票。

Both suggestions are dependent on users who use a javascript enabled or cookie enabled browser. 这两个建议都取决于使用启用javascript或启用cookie的浏览器的用户。

If you have to take bots into consideration who just send the url repeatetly to the server, you need to take a different approach - eg check whether the current ip address has called the url in the last n seconds and raise a 403 Forbidden http error or something. 如果你必须考虑将重复发送到服务器的网址,你需要采取不同的方法 - 例如检查当前的ip地址是否在最后n秒内调用了url并引发了403 Forbidden http错误或者一些东西。

If you can use setTimeout you can try something like this: 如果你可以使用setTimeout你可以尝试这样的事情:

var toggleVote = (function () {
  var voted = false,timeout;
  return function () {
     if( timeout )
       clearTimeout(timeout)

     voted = !voted

      timeout = setTimeout(function() {
        console.log("Ajax call - vote : " + voted)
      },400)
    }
  })()

document.getElementById("vote").addEventListener("click",toggleVote)

Heres an example on JSBin 下面是JSBin的一个例子

Try mashing the "Click me" div 尝试捣碎“点击我”div

Easy way: 简单的方法:

<button id="upvote" onclick="upvote(this)">Up</button>

<script>
   var upvote = function(x){
      $(x).removeAttr('onclick'); //if you click this button again, it will do nothing

      //call ajax to do something here...
      //...............
      //after finishing, add attribute onclick to the button
      $(x).attr('onclick', 'upvote(this)')
   };
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM