How to prevent user from login for 30 seconds after 3 failed login attempts - PHP

Поділитися
Вставка
  • Опубліковано 9 січ 2025

КОМЕНТАРІ • 65

  • @SPPR89
    @SPPR89 2 роки тому +1

    session can be deleted, so timeout can be avoided

  • @joseluisestevez4263
    @joseluisestevez4263 4 роки тому +1

    In my case I've got in $_SESSION['login_attempts'] the same value all the time. It seems the global variable is restablished all the time I execute the submit,. How can I keep the value of this variable? Thank you.

    • @AdnanAfzal565
      @AdnanAfzal565  4 роки тому

      May be you are not incrementing the value of this variable.

  • @moodzc945
    @moodzc945 2 роки тому

    Hello, i have a session_start() at the top of my code but it still shows an error of the following:
    Warning: Undefined array key "login_attempts" in ......
    What could be the problem?

    • @moodzc945
      @moodzc945 2 роки тому

      it seems like i should initialize the $_SESSION["login_attempts"] but how?

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      @@moodzc945 You need to put the following condition:
      if (isset($_SESSION["login_attempts"]))
      Also, make sure you are calling session_start() at the page where you are saving the value in this variable.

    • @nenavathkaveri4157
      @nenavathkaveri4157 2 роки тому

      did you initialized $_session["login_attempts"].if yes where you used that

  • @sumittiwari4667
    @sumittiwari4667 3 роки тому

    Please create this functionality with angular

  • @onlymeattheend3959
    @onlymeattheend3959 3 роки тому

    I'm having an error Undefined array key "login_attempts". Why??

    • @AdnanAfzal565
      @AdnanAfzal565  3 роки тому +1

      You need to use:
      session_start();
      if (isset($_SESSION["login_attempts"]))
      {
      // code goes here
      }

    • @AustinChris_
      @AustinChris_ 2 роки тому +1

      @@AdnanAfzal565 which codes goes there?

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      @@AustinChris_ That will be at the top of your file.

    • @AustinChris_
      @AustinChris_ 2 роки тому

      @@AdnanAfzal565 I did that, now I'm having an error of undefined index: login_attempts

  • @yeojsoriano8095
    @yeojsoriano8095 2 роки тому

    i have error undefined array key login attempts what does that mean?

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      Make sure you are running session_start() at the top.

    • @yeojsoriano8095
      @yeojsoriano8095 2 роки тому

      @@AdnanAfzal565 hello thank you for replying yes i have session start at the top

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      @@yeojsoriano8095 You can do iif (sset($_SESSION["login_attempts"])) {
      //
      }

  • @nenavathkaveri4157
    @nenavathkaveri4157 2 роки тому

    where is login_attempts is defined?

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      It is created here: ua-cam.com/video/tH7dzGnrSI8/v-deo.html

  • @NarenderKumar-rm4hm
    @NarenderKumar-rm4hm 4 роки тому

    I used the same code used by you... but got Notice: Undefined index: login_attempts in C:\xampp\htdocs\arc\attempt.php on line 61.. Please guide me.

    • @AdnanAfzal565
      @AdnanAfzal565  4 роки тому

      It means your _SESSION variable does not have that index. Make sure you are calling session_start at the top of file.

  • @yaredsisay1935
    @yaredsisay1935 3 роки тому

    I do get these warnings but the code does work. What could be the reason ? Thank you

  • @MegaJoGamer
    @MegaJoGamer Рік тому

    i tried many different ways to fix the Undefined array key "login_attempts" error but it is still not working. can you tell me how to fix this error please

    • @AdnanAfzal565
      @AdnanAfzal565  Рік тому

      You need to use the
      if(isset($_SESSION["login_attempts"])) {
      }

    • @sameddemir3351
      @sameddemir3351 Рік тому +3

      Hello, you need to add the @ symbol at the beginning of your code. For example: @$_SESSION['login_attempts'] += 1;
      This is not an error, it is a warning that we have received because the session named login_attempts has not been used yet.

  • @subrotomukherjee8571
    @subrotomukherjee8571 2 роки тому

    How to do the same task in Node js & MySQL ??

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      Node JS also has a module named “express-session”. Mysql queries will remain same.

    • @subrotomukherjee8571
      @subrotomukherjee8571 2 роки тому

      @@AdnanAfzal565 what is the R8 approach to do that pls help 🙏
      I am not getting any clue using express-session

  • @imrulemon8311
    @imrulemon8311 4 роки тому

    I am facing the problem
    Notice: Undefined index: login_attempts in C:\xampp\htdocs\web\user\login.php on line 35
    I have called session_start at the top of file
    How to fix it? Help plz

    • @AdnanAfzal565
      @AdnanAfzal565  4 роки тому

      Make sure you are calling isset($_SESSION["login_attempts"]) before using the session.

    • @intan8952
      @intan8952 4 роки тому

      @@AdnanAfzal565 hello, i also have the same problem and I already tried isset($_SESSION["login_attempts"]). but after 2 attempt the login button still appear and not disable. can I know what can I do?

    • @AdnanAfzal565
      @AdnanAfzal565  4 роки тому

      Make sure you are calling session_start(); at the top of your file.

    • @meseleawulachew8059
      @meseleawulachew8059 3 роки тому

      @@AdnanAfzal565 you are not called isset($_SESSION["login_attempts"]) in your source code. why it works?

    • @AdnanAfzal565
      @AdnanAfzal565  3 роки тому

      @@meseleawulachew8059 Because in my browser it was previously being saved.

  • @biswanathbanik9505
    @biswanathbanik9505 2 роки тому

    add countdown with the value

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому

      Can you please elaborate ?

    • @biswanathbanik9505
      @biswanathbanik9505 2 роки тому

      @@AdnanAfzal565 when you are showing the time 10 sec please add a count down like 10 9 8 7 every 1 sec it will be better

    • @AdnanAfzal565
      @AdnanAfzal565  2 роки тому +2

      Okay. Got it. Thank you for the suggestion.

  • @gautamdas-mh4vt
    @gautamdas-mh4vt Рік тому

    time function is not working

  • @ayazahamed8254
    @ayazahamed8254 Рік тому

    Well Explained.

  • @media7588
    @media7588 3 роки тому

    dont work

    • @AdnanAfzal565
      @AdnanAfzal565  3 роки тому

      What error you are getting ?

    • @dororo9748
      @dororo9748 8 місяців тому

      it work fine im not sure if youre code is right this is what i did:
      after post password i used lock out commands like this:
      $lockout_time = isset($_SESSION['lockout_time']) ? $_SESSION['lockout_time'] : 0;
      $login_attempts = isset($_SESSION['login_attempts']) ? $_SESSION['login_attempts'] : 0;
      then use if statement to check:
      if (time() - $lockout_time < 30) {
      echo " You have attempted to log in too many times. Please try again in " . (30 - (time() - $lockout_time)) . " seconds. ";
      } else {
      add remaindier code here and after then:
      } else {
      $login_attempts++;
      if ($login_attempts >= 3) {
      $_SESSION['lockout_time'] = time();
      }
      $_SESSION['login_attempts'] = $login_attempts;
      echo " You have entered the invalid username or a password, try again ";
      echo "Check";
      }
      }

    • @dororo9748
      @dororo9748 8 місяців тому +1

      this is what i did:
      $lockout_time = isset($_SESSION['lockout_time']) ? $_SESSION['lockout_time'] : 0;
      $login_attempts = isset($_SESSION['login_attempts']) ? $_SESSION['login_attempts'] : 0;
      if (time() - $lockout_time < 30) {
      echo " You have attempted to log in too many times. Please try again in " . (30 - (time() - $lockout_time)) . " seconds. ";

    • @dororo9748
      @dororo9748 8 місяців тому

      } else {
      $result = mysqli_query($con, "SELECT * FROM users WHERE Email='$email'");
      $row = mysqli_fetch_assoc($result);
      if ($row && password_verify($password, $row['Password'])) {
      $_SESSION['valid'] = $row['Email'];
      $_SESSION['username'] = $row['Username'];
      $_SESSION['age'] = $row['Age'];
      $_SESSION['id'] = $row['Id'];
      header("Location: home.php");
      exit;

    • @dororo9748
      @dororo9748 8 місяців тому

      } else {
      $login_attempts++;
      if ($login_attempts >= 3) {
      $_SESSION['lockout_time'] = time();
      }
      $_SESSION['login_attempts'] = $login_attempts;
      echo " Wrong Username or Password ";
      echo "Go Back";
      }
      }