1
In my system login, if the user has registered id and password the access is released. So far so good, but now I want to qualify this user in administrator(1) or simple user(0) to, when they log in, be redirected to different pages.
For that, I created a column "ativo"
to store the 0 or 1 in my user table and started writing a validation page that is not working yet:
<?php
// Primeiro verifica se o post não está vazio
if (!empty($_POST) AND !empty($_POST['identifiant']) OR !empty($_POST['senha'])) {
mysql_connect('localhost', 'root', '', 'db_formacao') or trigger_error(mysql_error());
// Tenta se conectar a um banco de dados MySQL
$identifiant = mysql_real_escape_string($_POST['identifiant']);
$senha = mysql_real_escape_string($_POST['senha']);
$ativo = mysql_real_escape_string($_POST['ativo']);
$sql = "SELECT `id`, `identifiant`, `senha`, `ativo` FROM `usuarios` WHERE (`identifiant` = '". $identifiant ."') AND (`senha` = '". $senha ."')";
$query = mysql_query($sql);
if (mysql_num_rows($query) != 1) {
// Mensagem de erro quando os dados são inválidos e/ou o usuário não foi encontrado
echo "Login inválido!"; exit;
} else {
$resultado = mysql_fetch_assoc($query);
// Verifica se o usuário é 0 ou 1
if ($resultado['ativo'] == 0)) { header("Location: principalUSU.php"); }
else { header("Location: principal.php"); }
exit;
}
}
?>
The login page is where the login form is and the action that connects with the validation page:
<!--CONTENT-->
<!--A parte do formulário-->
<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>
</div>
</div>
</div>
</div>
</div>
The table as in the database:
id | nome | senha | ativo
1 aaaa 12345 1
2 bbbb 12345 0
But when I try to turn the page erro 500
saying the page isn’t working.
Any idea why it doesn’t work?
BS: I changed my code to what appears here in the answer, but is still giving error 500.
To learn more about PHP and login security, I suggest this channel: https://www.youtube.com/watch?v=rCrf70u7hFI
– user24136
Thank you for answering! I copied your code, but it still gives error 500 when it falls on the validation page. It might be on the login page?
– Mariana Bayonetta