Php 带有AJAX的Jquery keyup事件导致错误结果

Php 带有AJAX的Jquery keyup事件导致错误结果,php,jquery,ajax,Php,Jquery,Ajax,我有以下Jquery代码,它侦听用户键入的captcha,并在每个keyup上发送ajax请求,以查看是否键入了正确的代码: $('#joinCaptchaTextBox').keyup(function() { $.get('scripts/ajax/script.php', { 'join_captcha': '1', 'captcha': $('#joinCaptchaTextBox').val()}, functi

我有以下Jquery代码,它侦听用户键入的captcha,并在每个keyup上发送ajax请求,以查看是否键入了正确的代码:

$('#joinCaptchaTextBox').keyup(function() {
    $.get('scripts/ajax/script.php', {
        'join_captcha': '1',
        'captcha': $('#joinCaptchaTextBox').val()},         
        function(data) {
            var obj = JSON.parse(data);
            if(obj.ajaxResponse.status) {
                $('#joinCaptchaNotAcceptable').hide();
                $('#joinCaptchaAcceptable').show();
            }else{
                $('#joinCaptchaAcceptable').hide();
                $('#joinCaptchaNotAcceptable').show();
            }
    });
});
另一端的PHP脚本只是检查会话并回复:

if($siteCaptcha == $_SESSION['secretword']) {
  $this->captchaCompare = TRUE;
}else{
  $this->captchaCompare = FALSE;
}
这在95%的情况下工作正常,但我发现有时它会报告键入的验证码不正确,即使它是正确的。我认为这可能是因为当键入fast时,许多请求被发送到服务器,而返回的订单或请求不是发送的订单,因此(只有一个是正确的)前一个订单最后收到,并且显示不正确

有更好的方法吗?是否有办法确保最后发送的请求最后收到?这里有我遗漏的东西吗。我可以提供更多信息


谢谢

添加超时,以便在用户键入fast时,不会在每次按键时发送请求:

$('#joinCaptchaTextBox').on('keyup', function() {
    clearTimeout( $(this).data('timer')  );
    $(this).data('timer', 
        setTimeout(function() {
            var data = {
                    join_captcha: '1',
                    captcha : $('#joinCaptchaTextBox').val()
            };

            $.ajax({
                url : 'scripts/ajax/script.php',
                data: data,
                dataType: 'json'
            }).done(function(result) {
                $('#joinCaptchaNotAcceptable').toggle(!result.ajaxResponse.status);
                $('#joinCaptchaAcceptable').toggle(result.ajaxResponse.status);
            });
        },500)
    );
});

这会有帮助,因为它会引入等待/延迟这是一个坏主意;这意味着一个脚本最多只需要发送25*个字符就可以强制执行您的验证码。换句话说,你会收到垃圾邮件;我以为到目前为止你在检查课文。但这仍然很糟糕。如果你弄错了,你所见过的每一个验证码都会变成一个新的验证码,这是有原因的。计算机视觉能够缩小可能的字母范围,如果你允许多次猜测(这种情况下),那么暴力可以应用于其余的可能性。