I have made a login in which I have everything in different files to organize it better, the problem comes when a user makes a mistake in the data and then enters the last else, which will reproduce an error text on the login page after redirection to her, but it doesn't and I've tried a thousand ways. As data, I use JQUERY dialogs, in case it has something to do with it.
dialog_login.php
<?php
require_once('login.php');
$error = $_POST['error'];
?>
<div id="dialog1" title="Login user" style="padding-top: 0;display: none;">
<form method="POST" action="login.php">
<fieldset class="display_block">
<label for="usuario_login">User</label>
<input type="text" name="usuario_login" id="usuario_login" placeholder="smith" autofocus required class="text ui-widget-content ui-corner-all">
<label for="password_login">Password</label>
<input type="password" name="password_login" id="password_login
" placeholder="1234567" required class="text ui-widget-content ui-corner-all">
<input type="submit" value="Login" tabindex="-1">
<p align="center" style="color: red;" ><?php echo isset($error) ? utf8_decode($error) : '' ; ?></p>
</fieldset>
</form>
</div>
login.php
<?php
require_once('conexion.php');
session_start();
if(!empty($_POST))
{
$usuario = mysqli_real_escape_string($mysqli,$_POST['usuario_login']);
$password = mysqli_real_escape_string($mysqli,$_POST['password_login']);
$error = '';
$sha1_pass = sha1($password);
$sql = "SELECT id, id_tipo FROM usuarios WHERE usuario = '$usuario' AND password = '$sha1_pass'";
$result=$mysqli->query($sql);
$rows = $result->num_rows;
if($rows > 0) {
$row = $result->fetch_assoc();
$_SESSION['id_usuario'] = $row['id'];
$_SESSION['tipo_usuario'] = $row['id_tipo'];
header("location: index.php");
} else {
$error = "User or password incorrect";
header("location: index.php");
}
}
PS: The index.php thing is because I have the web structured like this with requires importing all the parts of the web in it.
You can save the success of being logged in to a variable
$_SESSION
and then on the pageindex.php
check if you are logged in or not:Simplified example:
login.php
index.php