简体   繁体   English

检查PHP表单是否有效以便在3次尝试后锁定用户

[英]Checking if a PHP form is valid for the purpose of locking out a user after 3 attempts

I have a php login form with some authentication and validation. 我有一个PHP登录表单,其中包含一些身份验证和验证。 The particular script I have so far which I want to run is as follows: 到目前为止我要运行的特定脚本如下:

<script type="text/javascript">
var login_attempts=3;
function check_form()
 {
    //This is where I want the code to check if the form is valid and will be submitted
 {
   alert("SuccessFully Logged In");
   //Not sure if I need this as the form contains PHP so if the credentials are correct then tehy go to another page
 }

  else
      {
       if(login_attempts==0)
       {
       alert("No Login Attempts Available");
       }

    else
      {
      login_attempts=login_attempts-1;
      alert("Login Failed Now Only "+login_attempts+" Login Attempts Available");

    if(login_attempts==0)
       {
        document.getElementById("name").disabled=true;
        document.getElementById("pass").disabled=true;
        document.getElementById("form1").disabled=true;
  }
 }
}

return false;
}   
</script>

The reason I want this, is so that after a user has attempted to login 3 times I want an alert box to display a message and also stop the user from entering anything in the box. 我想要这个的原因是,在用户尝试登录3次后,我想要一个警告框来显示一条消息,同时也阻止用户输入框中的任何内容。 I don't actually want to lock the account from the DB end as the users are authenticating on information other than a username and password. 我实际上并不想从数据库端锁定帐户,因为用户正在对用户名和密码以外的信息进行身份验证。

I am quite novice when it comes to Js so sorry if I missed anything out 对于Js来说,我很新手很抱歉,如果我错过了什么

Edit: PHP login code 编辑:PHP登录代码

<?php
if($_POST)
  {
    include 'config.php';
$ref=$_POST['ref'];
$fullname=$_POST['fullname'];
$postcode=$_POST['postcode'];
$dob=$_POST['dob'];
$email=$_POST['email'];

$sUser=mysqli_real_escape_string($conn,$orielref);
$sPass=mysqli_real_escape_string($conn,$fullname);
$sPostcode=mysqli_real_escape_string($conn,$postcode);
$sDob=mysqli_real_escape_string($conn,$dob);
$sEmail=mysqli_real_escape_string($conn,$email);
$query="SELECT * From customers where ref_number='$sUser' and full_name='$sPass' and post_code='$sPostcode' and date_of_birth='$sDob' and email_address='$sEmail'";
$result=mysqli_query($conn,$query);
if(mysqli_num_rows($result)==1)
{   $row = $result->fetch_array(MYSQLI_ASSOC);
    session_start();
    $_SESSION['id'] = $row['id'];
    $_SESSION['ref_number'] = $row['ref_number'];
    header('location:index.php');
}
}

?>

First read ppl comments (are valid). 首先阅读ppl评论(有效)。 Moving over them (if you still want to) you need to make an ajax request to your login page. 移过它们(如果您仍然想要),您需要向登录页面发出ajax请求。

Since no info was provided regarding php response and method of calling I will suggest a solution but can't provide much code. 由于没有提供有关php响应和调用方法的信息,我将建议一个解决方案但不能提供太多代码。

If you use jquery take a look at http://api.jquery.com/jquery.ajax/ . 如果您使用jquery,请查看http://api.jquery.com/jquery.ajax/

Replace your first comment with: 将第一条评论替换为:

$.ajax({
  method: "POST",
  url: "/login.php",
  data: { username: $('#username').val(), pass: $('#pass').val() },
  success: function(response) {
     if (response.valid) {
         alert('Logged In');
     }
     else {
         login_attempts=login_attempts-1;
         alert("Login Failed Now Only "+login_attempts+" Login Attempts Available");
     }
  }
})

If you want vanilla you can check my answer about vanilla ajax (is old and I'm sure there may be better answeres to it but should still work) here: Insert external page html into a page html and adapt it to your needs. 如果你想要香草,你可以检查我关于香草ajax的答案(很老,我确信它可能会有更好的回答,但仍然可以工作)这里:将外部页面html插入页面html并根据你的需要进行调整。

If you edit your question to provide more detailed info I can provide a much better answer, but in its current state... that's pretty much all I can give without listing tens of possibilities. 如果你编辑你的问题以提供更详细的信息,我可以提供更好的答案,但在目前的状态......这几乎是我能给出的所有内容,而没有列出数十种可能性。

Edit: You added the login code: You need to return an answer to the client add to last line something like: 编辑:您添加了登录代码:您需要返回客户端的答案添加到最后一行,如:

echo json_encode(['valid' => true]); // If login successful

or 要么

echo json_encode(['valid' => false]); // If login not successful

Also you have sql injection possible there... really bad approach (but that's unrelated to current question). 你也有可能在那里进行SQL注入......非常糟糕的方法(但这与当前的问题无关)。 I suggest reading about pdo and prepared statements ( https://www.php.net/manual/en/book.pdo.php and https://www.w3schools.com/php/php_mysql_prepared_statements.asp ). 我建议阅读有关pdo和准备好的陈述( https://www.php.net/manual/en/book.pdo.phphttps://www.w3schools.com/php/php_mysql_prepared_statements.asp )。

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

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