限制在表单中输入的尝试

时间:2014-01-30 21:12:29

标签: php html mysql

如何设置用户输入一次性密码的次数? 如果用户第三次输入错误的密码,他将自动注销并需要再次登录。 我是否需要在SQL表上创建一个新的“尝试次数”列?

            <form method="POST" action="" onSubmit="return validate(this)" >
                <input type="button" value="Click for OTP" onclick="openotp()" />  <br/>  <br/> 

                    <table id="table">
                        <tr>
                            <td class="alt"><label for="otp">Enter the 6-digit iBanking OTP </label></td>
                            <td><input type="password" name="otp" maxlength="6"></td>
                        </tr>
                    </table>
                <br/>
                <input type="submit" name="submit" value="Click to submit OTP">
            </form>
                <?php
                $user_id = $_SESSION['user_id'];
             if(isset($_POST['submit'])){
                 $otp = $_POST['otp'];

                 $query = "SELECT otp FROM user where user_id='$user_id'";
                 $result = mysqli_query($link, $query) or die(mysqli_error($link));
                 $row = mysqli_fetch_array($result);
                 $rand = $row['otp'];

    if ($otp == $rand) {
    $query = "SELECT * FROM user WHERE user_id='$user_id' AND otp='$otp'";
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    echo "<script>location.href='index.php'</script>";


    } else  {
echo "Wrong password";
    }

             }

             ?>

1 个答案:

答案 0 :(得分:0)

使用会话变量

 $_SESSION['attempts'] 

然后在条件语句中,它无法将$ _SESSION ['尝试']增加1.然后在你的页面上说

  if ($_SESSION['attempts'] < 3)
  {
     //do stuff
  }
  else
  {
      echo "too many login failures";
  }