Я работал над php pdo для системы входа в систему на основе ролей. Я добавил дополнительный индекс в базу данных sql emp_active, и у меня есть идентификатор в основной записи emp_id, который я хочу включить в сеанс для использования при отслеживании изменений в системе. Любая помощь приветствуется. Введите следующий код:
INDEX PAGE Index.php
<?php
session_start();
$conn = new mysqli("localhost","root","","employee");
$msg="";
if(isset($_POST['login'])){
$emp_username = $_POST['emp_username'];
$emp_password = $_POST['emp_password'];
$emp_password = sha1($emp_password);
$empRole = $_POST['empRole'];
$sql = "SELECT * FROM employee WHERE emp_username=? AND emp_password=? AND emp_role=?";
$stmt=$conn->prepare($sql);
$stmt->bind_param("sss",$emp_username,$emp_password,$empRole);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
session_regenerate_id();
$_SESSION['emp_username'] = $row['emp_username'];
$_SESSION['role'] = $row['emp_role'];
session_write_close();
if($result->num_rows==1 && $_SESSION['role']=="standard"){
header("location:standard.php");
}
else if($result->num_rows==1 && $_SESSION['role']=="admin"){
header("location:admin.php");
}
else{
$msg = "User Data Entered is Invalid";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body class="bg-dark">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-5 bg-light mt-5 px-0">
<h3 class="text-center text-light bg-danger p-3">Login Page</h3>
<form action="" method="post" class="p-4">
<div class="form-group">
<input type="text" name="emp_username" class="form-control form-control-lg" placeholder="Username" required>
</div>
<div class="form-group">
<input type="password" name="emp_password" class="form-control form-control-lg" placeholder="Password" required>
</div>
<div class="form-group lead">
<label for="empRole">Select User Type:</label>
<select class="form-control" name="empRole">
<option value="standard" required>Standard</option>
<option value="admin" required>Admin</option>
</select>
</div>
<div class="form-group">
<input type="submit" name="login" class="btn btn-danger btn-block">
</div>
<h5 class="text-danger text-center"><?= $msg; ?></h5>
</form>
</div>
</div>
</div>
</body>
</html>
ADMIN PAGE admin.php
<?php
session_start();
if(!isset($_SESSION['emp_username']) || $_SESSION['role']!="admin"){
header("location:index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Admin Console</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-lg-1 mt-0 pt-1 px-0 bg-danger">
<h6 class="text-left text-dark p-1">Admin COnsole</h6>
</div>
<div class="col-lg-6 bg-danger pt-1 pb-1 mt-0 px-0 bg-danger">
<a href="admin.php" class="btn btn-dark btn-sm" role="button">Dashboard</a>
<a href="#" class="btn btn-dark btn-sm" role="button">Task</a>
<a href="adminperson.php" class="btn btn-dark btn-sm" role="button">Person</a>
<a href="adminemp.php" class="btn btn-dark btn-sm" role="button">Employee</a>
</div>
<div class="col-lg-4 bg-danger text-right pt-1 pb-1 pl-1 pr-1 mt-0 px-0 bg-danger">
LoggedUser: <?= $_SESSION['emp_username'] ?>
</div>
<div class="col-lg-1 bg-danger pt-1 pb-1 mt-0 px-0 bg-danger justify-content-right">
<a href="logout.php" class="btn btn-dark btn-sm justify-content-right" role="button">Logout</a>
</div>
</div>
<div class="row justify-content-center">
<div class="col-lg-2 bg-light mt-5 px-0">
<h3 class="text-center text-light bg-danger p-3">Admin Console</h3>
</div>
</div>
</div>
<body>
Продолжайте пробовать различные методы