Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript firebase中是否有防止快速连续写入的方法?_Javascript_Jquery_Firebase - Fatal编程技术网

Javascript firebase中是否有防止快速连续写入的方法?

Javascript firebase中是否有防止快速连续写入的方法?,javascript,jquery,firebase,Javascript,Jquery,Firebase,我编写了一个非常简单的轮询脚本,可以将投票保存到Firebase数据库。为了防止同一用户进行多次投票,我将电子邮件保存到一个单独的对象,然后在放置新条目之前进行检查(这是我自己的信息,不需要高安全性) 到目前为止,它似乎确实阻止了基本的犯规行为,但是我得到了许多快速连续多次投票的实例(可能比浏览器重新加载的速度更快)。在最坏的情况下,有16张选票是在几毫秒之间投下的 有没有办法防止在Firebase或JavaScript端进行这些快速连续写入 Firebase数据: { "votes" :

我编写了一个非常简单的轮询脚本,可以将投票保存到Firebase数据库。为了防止同一用户进行多次投票,我将电子邮件保存到一个单独的对象,然后在放置新条目之前进行检查(这是我自己的信息,不需要高安全性)

到目前为止,它似乎确实阻止了基本的犯规行为,但是我得到了许多快速连续多次投票的实例(可能比浏览器重新加载的速度更快)。在最坏的情况下,有16张选票是在几毫秒之间投下的

有没有办法防止在Firebase或JavaScript端进行这些快速连续写入

Firebase数据:

{
  "votes" : {
    " obj1 " : {
      "-KHXPWtcxzXhs2ULBE1Q" : {
        "name" : " name1 ",
        "email" : "email1",
        "timestamp" : 1463013744297
      },
      "-KHXPWuyhTg6S3Qcw4e9" : {
        "name" : " name1 ",
        "email" : "email1",
        "timestamp" : 1463013744382
      }
    },
    "obj2" : {
      "-KHZ20CRiT5fs6H4Nhel" : {
        "name" : "name2",
        "email" : "email2",
        "timestamp" : 1463041135613
      }
    },
  },
  "email" : {
    "-KHXPWtHSNYWBmvXsmNx" : {
      "email" : "email1",
      "timestamp" : 1463013744292
    },
    "-KHXPWus4hSwi1t00Gq_" : {
      "email" : "email1",
      "timestamp" : 1463013744377
    },
    "-KHZ20CdfcsoGjvn98Q" : {
      "email" : "email2",
      "timestamp" : 1463041135606
    },
  }
}
jQuery:

        var validateEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;

        function castVote() {

          var ref = new Firebase('firebaseurl');

          $( ".notification.exists" ).hide();
          $( ".notification.valid" ).hide();

          var obj = $('#obj').val().toLowerCase();
          var email = $('#email').val().toLowerCase();

          var emailref = ref.child("email");

          if (validateEmail.test(email)){
            emailref.once('value', function(snapshot) {
                var exists = false;
                snapshot.forEach(function(childSnapshot){
                    if(email === childSnapshot.val().email){
                        exists = true;     
                    }
                })

                if (exists){
                  $( ".notification.exists" ).show();
                }
                else{
                  write(obj,email,ref,emailref);
                }
            });
          }
          else{
            $( ".notification.valid" ).show();
          }
        }


        function write(obj,email,ref,emailref){

          var objref = ref.child("obj/" + obj);

          //save email
          emailref.push().set({
              email:email,
              "timestamp": Firebase.ServerValue.TIMESTAMP
            });

            //save vote
            objref.push().set({
              obj: obj,
              "email": email,
              "timestamp": Firebase.ServerValue.TIMESTAMP
            });

            //clear fields
            $('#obj').val('');
            $('#email').val('');
        }

      }

您应该使用安全规则在Firebase端防止这种情况。一个防止用户重新投票的简单规则可以是-

请注意,您需要将此代码段放在安全规则中的正确位置-

"votes" : {
   // keep .read/.write as applicable
   "$obj" : {
        "$user" : {
            // keep .read/.write as applicable
            // Don't allow write if the parent obj already contains this user entry
            ".validate" : "!data.parent().hasChild($user)"
        }
   }
}

这样,一旦您为某个
obj
将用户投票插入到
投票中,另一次插入将失败。

您应该使用安全规则在Firebase端防止这种情况发生。一个防止用户重新投票的简单规则可以是-

请注意,您需要将此代码段放在安全规则中的正确位置-

"votes" : {
   // keep .read/.write as applicable
   "$obj" : {
        "$user" : {
            // keep .read/.write as applicable
            // Don't allow write if the parent obj already contains this user entry
            ".validate" : "!data.parent().hasChild($user)"
        }
   }
}

这样,一旦您为某个
obj
将用户投票插入到
投票中,另一次插入将失败。

调用
write
时,检查冷却标志并返回
return
如果为真,则设置冷却标志,
setTimeout
解除延迟。调用
write
检查冷却标志并
return
如果为true,请设置冷却标志
setTimeout
解除延迟。由于我不知道您的完整JSON,我无法帮助您了解确切的安全规则。如果您可以从Firebase导出JSON并将其粘贴到这里,这会有所帮助。这种方法的问题是,在obj下有$user的地方,我使用的是push,因此有唯一的密钥,而不是可验证的用户;我不明白为什么,除非你知道你的用户是谁(使用Firebase身份验证),否则没有办法确保公平。在我看到的大多数投票场景中(我看到了很多),开发人员希望将其限制为“每个用户一票”。将其存储在@HazardouS descripes结构中是确保这一要求的最简单方法。我必须用下划线替换所有点,以便在firebase中用作ID,但这似乎效果良好。谢谢,因为我不知道您的完整JSON,我无法帮助您了解确切的安全规则。如果您可以从Firebase导出JSON并将其粘贴到这里,这会有所帮助。这种方法的问题是,在obj下有$user的地方,我使用的是push,因此有唯一的密钥,而不是可验证的用户;我不明白为什么,除非你知道你的用户是谁(使用Firebase身份验证),否则没有办法确保公平。在我看到的大多数投票场景中(我看到了很多),开发人员希望将其限制为“每个用户一票”。将其存储在@HazardouS descripes结构中是确保这一要求的最简单方法。我必须用下划线替换所有点,以便在firebase中用作ID,但这似乎效果良好。谢谢