如何根据角色为多个页面设置php会话

时间:2018-08-30 02:29:27

标签: php session mysqli

<?php
session_start();
if (empty(isset($_SESSION['user']))) {
    header('location:index.php');
}
if (isset($_SESSION['role']) == 'admin') {
    header('location:admin.php');
}
if (isset($_SESSION['role']) == 'agent') {
    header('location:user.php');
}
if (isset($_SESSION['role']) == 'supervisor') {
    header('location:supervisor.php');
}

?>
<html>
<head>
<title>PHP MySQL Role Based Access Control</title>
</head>
<body>
    <div align="center">
        <h3>PHP MySQL Role Based Access Control</h3>
        <form method="POST" action="includes/login.php">
            <table>
                <tr>
                    <td>UserName:</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>PassWord:</td>
                    <td><input type="text" name="password" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="login" value="Login" /></td>
                </tr>
            </table>
        </form>
<?php if(isset($error)){ echo $error; }?>
</div>
</body>
</html>

//如果尝试在各种登录用户中添加一些会话,如果他们在注销之前意外关闭了浏览器窗口,则应将其重定向到引用页面而不是索引页面

1 个答案:

答案 0 :(得分:0)

尝试一下。希望对您有帮助。

session_start();
 if (isset($_SESSION['user']['user_id'])) {
            if ($_SESSION['user']['role'] == 'admin') {
                header('location:admin.php');

            } else if ($_SESSION['user']['role'] == 'agent') {
               header('location:user.php');

            } else if ($_SESSION['user']['role'] == 'supervisor') {
               header('location:supervisor.php');
            } 
相关问题