Php 如何检查用户记录的可用性,如果可用,如何重新输入以确保安全?

Php 如何检查用户记录的可用性,如果可用,如何重新输入以确保安全?,php,jquery,ajax,Php,Jquery,Ajax,我们需要使用Ajax调用php文件,以检查数据库中记录的可用性 如果记录存在,请用户再次在另一个框中输入,以重新确认令牌号 如果匹配,用户将继续填写表单的其余部分 如果它们不匹配,则会引发错误 我有两个独立的文件,它们实现了双重目标:检查数据库的可用性,并通过要求用户重新输入相同的数字来比较记录 但是,我想将这两个文件合并为一个文件 有人能给我引路吗 第一个文件使用ajax调用php文件以检查记录的可用性 它被称为index.html <!DOCTYPE HTML PUBLIC "-//W

我们需要使用Ajax调用php文件,以检查数据库中记录的可用性

如果记录存在,请用户再次在另一个框中输入,以重新确认令牌号

如果匹配,用户将继续填写表单的其余部分

如果它们不匹配,则会引发错误

我有两个独立的文件,它们实现了双重目标:检查数据库的可用性,并通过要求用户重新输入相同的数字来比较记录

但是,我想将这两个文件合并为一个文件

有人能给我引路吗

第一个文件使用ajax调用php文件以检查记录的可用性

它被称为index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Check Availability of Token</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">
$(document).ready(function()
{
    $("#token").blur(function()
    {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(1000);
        //check the token exists or not from ajax
        $.post("checkToken.php",{ user_token:$(this).val() } ,function(data)
        {
          if(data=='no') //if token not avaiable
          {
            $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
            {
              //add message and change the class of the box and start fading
              $(this).html('Token exists').addClass('messageboxerror').fadeTo(900,1);
            });
          }
          else
          {
            $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
            {
              //add message and change the class of the box and start fading
              $(this).html('Token does not exist').addClass('messageboxok').fadeTo(900,1);
            });
          }

        });

    });
});
</script>

</head>
<body>


<br>
<br>
<div align="center">
<div class="top" > Check Availability of token - <strong>Token &quot;roshan&quot;, &quot;mike&quot; ,&quot;jason&quot; exists</strong><br>
  Please move the focus out of the box to check the availability of token.
</div>
<div >
   Token : <input name="token" type="text" id="token" value="" maxlength="15" />
   <span id="msgbox" style="display:none"></span>
</div>

</div>

</body>
</html>

检查令牌的可用性
$(文档).ready(函数()
{
$(“#令牌”).blur(函数()
{
//删除所有类添加messagebox类并开始淡入淡出
$(“#msgbox”).removeClass().addClass('messagebox').text('Checking…').fadeIn(1000);
//检查ajax中是否存在令牌
$.post(“checkToken.php”,{user_token:$(this).val()},函数(数据)
{
if(data='no')//如果令牌不可用
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this.html('Token exists').addClass('messageboxerror').fadeTo(900,1);
});
}
其他的
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this).html('Token不存在').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});


检查令牌的可用性—令牌“roshan”、“mike”、“jason”存在 请将焦点移出框以检查令牌的可用性。 代币:
第二个文件名为tokencompare.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - Compare Validate token</title>
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#confirm_token").parent().hide();
$("#token").keydown(function() {
    var self = this, $self = $(this);
    setTimeout(function(){
        if ( $.trim(self.value) != "" && !$self.is(".error") ) {
            $("#confirm_token").parent().show();
            return;
        }
        $("#confirm_token").val('').parent().hide();
    },0);
});

// validate signup form on keyup and submit
$("#tokenval").validate({
    rules: {
        token: {
            required: true,
            minlength: 5
        },
        confirm_token: {
            required: true,
            minlength: 5,
            equalTo: "#token"
        }
    },
    messages: {},
    submitHandler: function(form) {
        alert("submitted");
    }
});
});//]]>

</script>
</head>
<body>
  <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
<form class="cmxform" id="tokenval" method="get" action="">
    <fieldset>
        <legend>Validating a complete form</legend>
        <p>
            <label for="token">token</label>
            <input id="token" name="token" type="text" />
        </p>
        <p>
            <label for="confirm_token">Confirm token</label>
            <input id="confirm_token" name="confirm_token" type="text" />
        </p>
        <p>
            <input class="submit" type="submit" value="Submit" />
        </p>
    </fieldset>
</form>
</body>
</html>

-比较验证令牌
//
验证完整表单

代币

确认令牌

谢谢您的帮助

好的,让我们看看这是否符合您的要求

“真正”的代币是xxxxx

任何令牌的格式为五个字符。代码将带前导零和尾随零

所以你可以从xxxxx开始,看看会发生什么

然后是xxx

然后rrr

然后RRR

JS

var validtoken=“xxxxx”;
$('#checkme')。单击(函数(){
$('#messagediv').html('');
var firsttoken=$('#token1').val();
firsttoken=firsttoken.trim();
if(firsttoken.length!==5)
{
$('#messagediv').html('令牌的格式不正确
请重新输入'); $('#token1').val(''); }否则{ if(firsttoken==validtoken) { $('#messagediv').html('您的令牌存在
单击继续进行下一步'); $('.procedure').css('display','block'); }否则{ $('.errordiv').css('display','block'); } } A} ); $(“#继续”)。单击(函数(){ $('#token1').val(''); $('.procedure').css('display','none'); $('#messagediv').html(''); 警报(“下一步是……”); }); $('#确认')。单击(函数(){ var confirmtoken1=$('#token1').val(); var confirmtoken2=$('#token2').val(); if(confirmtoken1==confirmtoken2) {$('#token1').val(''); $('#token2').val(''); $('.procedure').css('display','none'); $('#messagediv').html(''); 警报(“已确认。下一步是…”); }否则{ $('#token1').val(''); $('#token2').val(''); $('.procedure').css('display','none'); $('.errordiv').css('display','none'); $('#messagediv').html('tokens not match.Reenter'); } });
如何选择第一个令牌?@TimSPQR,用户在文本框中输入第一个令牌。这在index.html上。然后,当焦点从框中移开(onblur)时,就会发出一条关于令牌是否存在的消息。在php文件上,如果存在,则results=“yes”,否则,results=“no”我对php位没有意见。谢谢你,嗯……所以用户只需一次性输入令牌,如果存在,请确定,如果不确定。因此,一个ajax查询可以从数据库中提取整个行,它存在或不存在。如果他们已经输入了令牌,为什么他们需要重新确认令牌?为什么不让他们输入两次,比如密码更改操作?然后给db打个电话。@TimSPQR,这是个很好的问题。我自己也问了同样的问题。事实上,如果您运行我发布的第二个名为tokencompare.html的文件,它就是这样做的。他们唯一的问题是,当令牌插入数据库时,用户通过电子邮件收到令牌。因此,当用户输入令牌时,他们希望db验证是的,这是一个有效的令牌,然后重新输入以确保安全。没有道理,但我正在尝试看看它是否可行。感谢againOk,所以这两个令牌条目是由时间(短或长)分隔的,因此您可能需要进行双重查询,除非时间总是很短。你可以在本地存储一个变量,并且希望用户不要刷新屏幕,或者你可以做一个cookie。这确实是一个很好的工作。但问题是,在准备提交到数据库之前,我们不想单击按钮。如果我们取消一个步骤会更容易吗?而不是co
var validtoken = "xxxxx";

$('#checkme').click(function(){
     $('#messagediv').html('');
     var firsttoken = $('#token1').val();
     firsttoken = firsttoken.trim();

    if( firsttoken.length !== 5)
      {
       $('#messagediv').html('The token has an incorrect format<br /> Please reenter');
       $('#token1').val('');    
       } else {
                if (firsttoken == validtoken)
                  {
                   $('#messagediv').html('Your token exists<br /> click PROCEED for the next step');
                   $('.proceed').css('display', 'block');
                   } else {
                   $('.errordiv').css('display', 'block');
                           }
               }
                             A});

$('#proceed').click(function(){
  $('#token1').val('');     
  $('.proceed').css('display', 'none');
  $('#messagediv').html('');
  alert('And the next step is....');
                                });

$('#confirm').click(function(){
  var confirmtoken1 = $('#token1').val();
  var confirmtoken2 = $('#token2').val();

  if(confirmtoken1 == confirmtoken2)    
    { $('#token1').val('');  
      $('#token2').val('');      
      $('.proceed').css('display', 'none');
      $('#messagediv').html('');
      alert('Confirmed. And the next step is....');
    } else {
            $('#token1').val('');  
            $('#token2').val('');      
            $('.proceed').css('display', 'none');
            $('.errordiv').css('display', 'none');
            $('#messagediv').html('tokens do not match. Reenter');
           }
                              });