如何在 php 中基于角色创建多用户(管理员、注册用户、普通访客)登录系统?

How to create multi user (Admin, Registered User, General Visitor) login system on Role Based in php?

我正在尝试为管理员、注册用户和普通访客(非注册用户)创建 3 个不同的部分。我在很多地方搜索,但没有找到任何信息。这里我给非注册用户和注册用户我的代码。

请问 customize/edit 我的 Admin 部分代码

提前致谢

我的索引页:

<?php
require 'header.php';
require 'includes/dbh.inc.php';
?>

<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Home Page</title>


<main>

<?php 

if (isset($_SESSION['userId'])) {
    echo '<p>You are Loged in!</p>';

}
else{
    echo '<p>You are Loged out!</p>';
}

?>          

</main>

<?php
require 'footer.php';
?>

我的login.inc页面:

<?php 

if (isset($_POST['login-submit'])) {

require 'dbh.inc.php';

$mailuid = $_POST['mailuid'];
$password = $_POST['pwd'];

if (empty($mailuid) || empty($password)) {
    header("Location: ../login.php?error=emptyfields");
    exit();
}
else{
    $sql = "SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../login.php?error=sqlerror");
        exit();
    }
    else{
        mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        if ($row = mysqli_fetch_assoc($result)) {
            $pwdCheck = password_verify($password, $row['pwdUsers']);
            if ($pwdCheck == false) {
                header("Location: ../login.php?error=wrongpwd");
                exit(); 
            }
            else if ($pwdCheck == true) {
                session_start();
                $_SESSION['userId'] = $row['idUsers'];
                $_SESSION['userUid'] = $row['uidUsers'];


                header("Location: ../index.php?login=success");
                exit();
            }
            else{
                header("Location: ../login.php?error=wrongpwd");
                exit(); 
            }

        }
        else{
            header("Location: ../login.php?error=nouser");
            exit();
        }
    }
 }

 }
else{
header("Location: ../login.php");
exit();
}

您必须将每个用户的角色添加到数据库中,然后在登录检查后设置另一个具有角色值的 $_SESSION。示例:

$_SESSION['role'] = $row['role']

然后在你需要的每一页检查这个值。