dsfs23434 2015-11-25 04:56
浏览 27
已采纳

记录三次失败的登录尝试

I need a script for logging login attempts: if a user attempts to login 3 times with the wrong password, then script will alert user to log in another way.

I tried this way but it's not working. How should I make this work? An explanation of the implementation of the code would also be appreciated.

public function log($usrname, $password, $type) {

    try {
        $stmt = $this->db->prepare("SELECT * FROM $table WHERE username=:username and password =:password and type=:type");
        $stmt->execute(array(':username' => $usrname, ':password' => $password, ':type' => $type));
        $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($stmt->rowCount()== 1) {
            echo "loged in";
            } else if($stmt->rowCount == 0){
                $ec = 0;

                $error = ++$ec;
                if(count($error)== 3){
                    echo "you provide wrong username and password 3 times";
                }
            }
        }
    } catch (PDOException $e) {
        $e->getMessage();
    }
}
  • 写回答

2条回答 默认 最新

  • doumei7420 2015-11-25 05:04
    关注

    You should have a 'attempt' field in the database table and each time you login with a wrong password just update the count into the table field and you can check the attempt count in the error condition. if the login is success just reset the attempt count to 0.

    I've provide the code logic here. but I am not tested it. use this logic.

    But I suggest you to check the username is registered with your table

     public function log($usrname, $password, $type) {
    
            try {
    
                $stmt0 = $this->db->prepare("SELECT username FROM $table WHERE username=:username");                
                $stmt0->execute(array(':username' => $usrname);
                if ($stmt0->rowCount()== 1) {
    
                      $stmt = $this->db->prepare("SELECT * FROM $table WHERE username=:username and password =:password and type=:type");
                      $stmt->execute(array(':username' => $usrname, ':password' => $password, ':type' => $type));
                      $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
                      if ($stmt->rowCount()== 1) {
                          echo "loged in";
                          // UPDATE THE attempt field to 0 here
                           $stmt2 = $this->db->prepare("UPDATE $table SET attempt=:attepmt WHERE username=:username");
                           $stmt2->execute(array(':username' => $usrname, ':attempt' => 0));
    
                      }else if($stmt->rowCount == 0){                    
                               // select the attempt field and check this is 3
                              $stmt3 = $this->db->prepare("SELECT attempt FROM $table WHERE username=:username");
                              $stmt3->execute(array(':username' => $usrname));
                              $obj= $stmt3->fetch(PDO::FETCH_OBJ);
                              if($obj->attempt==3){                           
                                  echo "you provide wrong username and password 3 times";
                              }
                          $attempt = $obj->attempt+1;
                          $stmt4 = $this->db->prepare("UPDATE $table SET attempt=:attepmt WHERE username=:username");
                          $stmt4->execute(array(':username' => $usrname, ':attempt' => $attempt));
    
    
                      }
    
    
                }else{
                  echo "Username  is not  found in table !";
                }
            } catch (PDOException $e) {
                $e->getMessage();
            }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题