Check if the user is active in the database

Asked

Viewed 739 times

0

I need to make a query if the user is active. Ex.

I client log in to the panel, if my status is disabled, shows me a message, if my status is active log in to the page.

follows my code

    <?php
    // inclui o arquivo de inicialização
    include_once('assets/conn/init.php');

    // resgata variáveis do formulário
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $password = isset($_POST['senha']) ? $_POST['senha'] : '';
    // cria o hash da senha
    $passwordHash = make_hash($password);

    $PDO = db_connect();
    $sql = "SELECT * FROM usuarios WHERE email = :email AND senha = :password";
    $stmt = $PDO->prepare($sql);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':password', $passwordHash);
    $stmt->execute();

    $users = $stmt->fetchAll(PDO::FETCH_OBJ);

    //Se o email não existir na base de dados gera um alerta
    if (count($users) <= 0):
        header('Location: index.php?msg=1');
        exit;
    endif;

    // pega o primeiro usuário
    $user = $users[0];
     session_start();
    $_SESSION['logged_in'] = true;
    $_SESSION['user_id'] = $user['usuario_id'];
    $_SESSION['nome'] = $user['nome'];
    $_SESSION['email'] = $user['email'];
    $_SESSION['nivel_usuario'] = $user['nivel_usuario'];
    header('Location: home.php');
  • One solution is to insert into your table usuários a field status where for example the value 1 would set an active user and 0, inactive. When playing all users of the query in the array users, you can check the field value status, depending on this, you show the inactive alert, if you are.

  • In the database I already have this column that is called "active", as I do this application in my code?

2 answers

0

Assuming you already have the active column created and the values are boolean 0 or 1, do so:

//.............
//Se o email não existir na base de dados gera um alerta
if (count($users) <= 0):
    header('Location: index.php?msg=1');
    exit;
endif;

// pega o primeiro usuário
$user = $users[0];

if($user['ativo'] == 0){
    header('Location: index.php?msg=Esse usuário não está ativo');
    exit;
}

0


You can check whether the user is active or not using the table field called status

Example:

//Se o email não existir na base de dados gera um alerta
if (count($users) <= 0):
    header('Location: index.php?msg=1');
    exit;
endif;

$user = $users[0];
// 1 ativo
// 2 Inativo
if ($user['status'] == 1):
    // pode ser usado um redirect
    header('Location: index.php?msg=2'); 
    exit;
endif;
  • I had not seen the answer of the above colleague... but how good that we are correct!

  • Obg manow... gave it right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.