0

I went through this login system with multi-users. It's working fine since it doesn't allow my status_id users '2' to login (inactive status), but when this happens I get the echo message twice on screen.

What am I doing wrong? I want to validate both user/password, user_type (admin/user) and user_status (1-active, 2-inactive).

<?php

include 'database/connect.php';

if (isset($_POST["submit"])) {
    $email = $_POST["txtemail"];
    $pass = $_POST["txtpass"];

    $query = mysqli_query($con, "SELECT user_email,user_password,user_type_id, status_id FROM user");
    while ($row = mysqli_fetch_array($query)) {
        $db_email = $row["user_email"];
        $db_pass = $row["user_password"];
        $db_type = $row["user_type_id"];
        $db_user_status = $row['status_id'];

        if ($email == $db_email && $pass == $db_pass && $db_user_status == '1') {
            session_start();
            $_SESSION["email"] = $db_email;
            $_SESSION["type"] = $db_type;

            if ($_SESSION["type"] == '1') {
                header("Location:admin/home_admin.php");
            } else {
                header("Location:user/home_user.php");
            }
        } else {
            echo "Ups. Algo de errado aconteceu.";
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Jun 06 '20 at 15:18
  • thanks. will take more atention to that. – Gonçalo Ferreira Jun 06 '20 at 21:49

2 Answers2

0

Well it looks like you are looping through every user inside your user table, so the posted email and password can only be right for one user and for the rest of them your program will go through the else statement

Jesse Jut
  • 349
  • 3
  • 5
0

Looking at your code, if the conditions specified inside the loop fails then the else will execute.

So if your user table holds 3 records and all 3 records doesn't satisfy the condition specified it will execute else statement and 3 times.

This might be the reason.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Siddhant
  • 196
  • 2
  • 9