3
I have a login system that, if the user is an administrator, will be redirected to one page and if common, to another.
That column ativo
is what defines whether a user is admin(1) or not(0).
identifiant | senha | ativo
aaaaaaaaaaaa 12345 1
bbbbbbbbbbbb 12345 0
The login page:
<!--A parte do formulário-->
<?php session_start(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading" style="
margin-top: 14px;">
<h3 class="panel-title">Login</h3>
</div>
<div class="panel-body" style="background: rgba(32, 40, 76, 0.59);">
<?php
if(isset($erro))
if(count($erro) > 0){ ?>
<div class="alert alert-danger">
<?php foreach($erro as $msg) echo "$msg <br>"; ?>
</div>
<?php
}
?>
<form method="post" action="validacao.php" role="form">
<fieldset style="background: #9498a9;">
<div class="form-group">
<input class="form-control" placeholder="Identifiant" name="identifiant">
</div>
<div class="form-group">
<input class="form-control" required placeholder="Senha" name="senha" type="password" value="">
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Lembrar-me
</label>
</div>
<button type="submit" name="login" value="true" class="btn btn-success btn-block" style="background: #232b4f; border-color: #e2e2e2;">Login</button>
</fieldset>
</form>
<p class="text-center text-danger">
<?php if(isset($_SESSION['loginErro'])){
echo $_SESSION['loginErro'];
unset($_SESSION['loginErro']);
}?>
</p>
<p class="text-center text-success">
<?php
if(isset($_SESSION['logindeslogado'])){
echo $_SESSION['logindeslogado'];
unset($_SESSION['logindeslogado']);
}
?>
</p>
</div>
</div>
</div>
</div>
</div>
The validation page:
<?php
session_start();
//Incluindo a conexão com banco de dados
include_once("conexao.php");
//O campo usuário e senha preenchido entra no if para validar
if((isset($_POST['identifiant'])) && (isset($_POST['senha']))){
$usuario = mysqli_real_escape_string($conn, $_POST['identifiant']); //Escapar de caracteres especiais, como aspas, prevenindo SQL injection
$senha = mysqli_real_escape_string($conn, $_POST['senha']);
//Buscar na tabela usuario o usuário que corresponde com os dados digitado no formulário
$result_usuario = "SELECT * FROM usuarios WHERE identifiant = '$identifiant' and senha = '$senha' LIMIT 1";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$resultado = mysqli_fetch_assoc($resultado_usuario);
//Encontrado um usuario na tabela usuário com os mesmos dados digitado no formulário
if(isset($resultado)){
$_SESSION['id'] = $resultado['id'];
$_SESSION['ativo'] = $resultado['ativo'];
$_SESSION['identifiant'] = $resultado['identifiant'];
if($_SESSION['ativo'] == "1"){
header("Location: principal.php");
}elseif($_SESSION['ativo'] == "0"){
header("Location: principalUSU.php");
}
//Não foi encontrado um usuario na tabela usuário com os mesmos dados digitado no formulário
//redireciona o usuario para a página de login
}else{
//Váriavel global recebendo a mensagem de erro
$_SESSION['loginErro'] = "Usuário ou senha Inválido";
header("Location: login.php");
}
//O campo usuário e senha não preenchido entra no else e redireciona o usuário para a página de login
}else{
$_SESSION['loginErro'] = "Usuário ou senha inválido";
header("Location: login.php");
}
?>
The big problem is that even if you enter the right login and password, it says that login or password are invalid and redirects you to the login page.
NOTE: This question looks like one I asked, but the code is another, I preferred not to edit that question because here the code is structured in a different way and I didn’t want to lose the previous code.
You are mixing "MYSQLI API" with "MYSQL API". This will give problem. In the other question the code although using both Apis they are in different files and there probably works, the code here no longer has to work, I recommend that it stays in the other one that at least is not a "typo".
– Guilherme Nascimento