Blocked the user after 3 attempts in PHP?

五迷三道 提交于 2019-12-04 23:29:47

Don't use cookies as the hacker can still disable cookies and continue brute force attacks. Use your database instead. For every failed attempt, log it into the table, together with a time stamp. Then on each request, do a query with the user ID and time stamp, then get the count. That should give you the number of times tried.

shygoo

A similar Q&A can be found in this thread: Increment a database field by 1

The login attempts should actually be stored separately from the user table. You can block login attempts by storing the number of attempts client side (putting the number of attempts in a cookie) or by storing the IP of the user trying to log in with the number of attempts server side (in your sql database); incrementing the number of attempts per each failed login. The cookie method has a drawback of being easily circumvented by an attacker. The IP address method is more difficult to circumvent but has a drawback of blocking people sharing the same IP. I would use a little bit of both methods; applying rules like 3 attempts for the cookie and 15 attempts for the IP address/attempts table.

IP address variable: $_SERVER["REMOTE_ADDR"];
Getting cookies: $_COOKIE["cookie_name"];
Setting cookies: setcookie("cookie_name", "value", time()+$seconds);

Basic implementation of the cookie method:

// before login attempt:
$attempt_count = intval(@$_COOKIE["login_count"]); 
if($attempt_count > 3){
   die("Too many attempts");
}

_

// on failed login attempt:
setcookie("login_count", $login_count+1, time()+600);
// cookie set to expire at (now+600 seconds); 10 minutes

http://php.net/manual/en/function.setcookie.php

First when the user tries with incorrect password you have to increment login attempt field by one. Then check the login attempt count equals to 3 or not. if it is 3 means then block the user by changing their status

    $result = mysqli_query($con, "SELECT * FROM Users WHERE contractNumber='$cnumber' AND password='$password'");
    $data = mysqli_num_rows($result);
    if($data==1)
    {
          $_SESSION['login_user']=$cnumber; // Initializing Session
          header('Location: profile.php');
    } 
    else
    {
          mysqli_query($con, "UPDATE tablename login_attempts = login_attempts+1 WHERE contarct_number = ". $cnumber");
          /*Select query login attempts and check the count*/


    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!