2016-11-02 67 views
-3

如何在3次登录尝试后阻止用户?如何在3次登录尝试后阻止用户?

这里是我的代码:

session_start(); 

     /************Connexion************/ 

    if(isset($_POST['cnx'])){ 
    require_once('../config.php'); 
    $db = new DBSTOCK(); 
    $cnx = $db->connect(); 
    $user=$_POST['user']; 
    $pass=$_POST['pass']; 
    // To protect MySQL injection for Security purpose 
    $user = strip_tags($user); 
    $pass = strip_tags($pass); 

    $user = stripslashes($user); 
    $pass = stripslashes($pass); 

    $user = mysqli_real_escape_string($cnx,$user); 
    $pass = mysqli_real_escape_string($cnx,$pass); 

    $q=mysqli_query($cnx,"select * from admin where user='".$user."'"); 

    $row = mysqli_fetch_array($q); //or die(mysqli_error($con)); 
    $pw = $row['pass'];//hashed password in database 
    $username = $row['user']; 


    if($user==$username && password_verify($pass, $pw)) { 
    $_SESSION["user"]=$user; 
    header("Location: ../view/accueil.php"); 
    } 
    else{ 
    header("Location: ../index.php?failed=0"); 
    }} 


     /************Deconnexion************/ 

     if(isset($_GET['decnx'])){ 

     session_destroy(); 
     session_unset(); 

     header("Location: ../index.php"); 
    } 

任何脚本的建议,我可以添加到我的代码,以便用户可以阻断10分钟后连续3次失败的登录尝试?

+2

请看看[这里](http://stackoverflow.com/questions/30770438/blocked-the-user-after-3-attempts-in-php)和[here](http:// stackoverflow。 com/questions/14035845/how-to-blocked-login-a-minutes-after-3-successful-login) – crowchirp

回答

4

以下两列添加到您的行:

  • last_attempt为datetime
  • attempt_count为int

在你登录逻辑,检查这两个值,如果是3或更多和在时间范围内(例如:10分钟),然后更新last_attempt并递增attempt_count,第二部分不是必需的,但您可能想知道这一点。如果超过10分钟,则将其设置为0,否则将其重新更新为last_attempt。

作为奖励,您现在也知道用户上次登录的时间,当您想要查找未使用的帐户时这很有用。

+0

您能否举个例子 –

+1

@baktetemiloud我刚才明确解释了该怎么做,我不是要为你做你的工作,如果你不能把概念转化为代码,你永远不会学习。 – TravisO

+0

昨晚我正在研究这个解决方案,比饼干更好谢谢 –

0

有很多方法可以做到这一点。如果你想阻止设备,那么你可以创建一个cookie 10分钟,并设置一个条件,如果用户名匹配,那么它不会击中数据库登录。

或者,如果你想阻止任何设备用户,那么你必须保持用户状态,连续三次失败尝试将改变用户状态和被阻止的时间。但是这次你要检查阻塞的时间是否在10分钟之前,或者不是每次登录尝试。

+0

你会更好地使用服务器端锁定,而不是客户端(带有cookie),因为cookie可以由用户修改。 TravisO的回答非常简单,可以说更安全。 :) – XtraSimplicity

相关问题