You can use session_start()
and create the session variables ( $_SESSION
) with which you will verify that if this session variable is defined you will let it access the page, in your case it is adminStock.php
.
Example:
<?php
require('conexion.php');
session_start();
if(isset($_SESSION["id_usuario"])){
header("Location: adminStock.php");
}
if(!empty($_POST))
{
$usuario = mysqli_real_escape_string($mysqli,$_POST['usuario']);
$password = mysqli_real_escape_string($mysqli,$_POST['password']);
$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: adminStock.php");
} else {
$error = "El nombre o contraseña son incorrectos";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" >
<div><label>Usuario:</label><input id="usuario" name="usuario" type="text" ></div>
<br />
<div><label>Password:</label><input id="password" name="password" type="password"></div>
<br />
<div><input name="login" type="submit" value="login"></div>
</form>
<br />
<div style = "font-size:16px; color:#cc0000;"><?php echo isset($error) ? utf8_decode($error) : '' ; ?></div>
</body>
</html>
And within adminStock.php
before DOCTYPE
of html you should verify that there is said session variable otherwise you will return it to the user's login.
Code:
<?php
session_start();
if (!isset($_SESSION['id_usuario'])) {
header("Location: index.php");
}
?>
To handle different Stocks
first of all you must have a structure in your database to be able to handle all that information and that the table where you manage the stock has a llave foránea
pointing to the id del usuario
so that at the moment the user enters you can make a select
of all the stock information of that user with a clause where
.