dsjgk330337 2018-12-19 17:00
浏览 72
已采纳

基于角色的登录在php中

I have created a page in php which allows the users to update,create and delete the list. There are two types of users, admin and student. I want to allow the students only to view the list and allow the admin to perform other operation. I have created the table to view the actions. How can I separate it for different users? I cannot redirect the users based upon their role because user needs to click button to perform action.

    <?php
    include 'Database.php';
        $query=mysqli_query($con,"SELECT * FROM student");
        while ($res=mysqli_fetch_array($query)) {

            ?>
            <tr>
                <td><?php echo $res['id']; ?></td>
                <td><?php echo $res['Username']; ?></td>
                <td><?php echo $res['Email']; ?></td>
                <td><?php echo $res['Course']; ?></td>
                <td> <button class="w3-btn w3-red"> <a href="delete.php?id=<?php echo $res['id']; ?>"> Delete </a>  </button>
                 <button class="w3-btn w3-blue"> <a href="update.php?id=<?php echo $res['id']; ?>"> Update </a> </button> </td>
            </tr>
            <?php
        }
    ?>
</table>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dtot74529 2018-12-19 17:21
    关注

    Let's first create a table for users:

    CREATE TABLE `users` (
      `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `user_email` varchar(100) NOT NULL DEFAULT '',
      `user_pass` varchar(255) NOT NULL DEFAULT '',
      `user_role` ENUM('admin', 'student') NOT NULL DEFAULT `student`,
      `user_registered` datetime NULL DEFAULT NULL,
      `user_status` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`ID`),
      KEY `user_email` (`user_email`)
    );
    

    Then the login page:

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $email = $_POST['email'];
        $pass  = $_POST['pass'];
    
        ...
    
        // Save user into $_SESSION
        $_SESSION['user'] = $user;
    }
    

    View users page:

    <tr>
        <td><?php echo $res['id']; ?></td>
        <td><?php echo $res['Username']; ?></td>
        <td><?php echo $res['Email']; ?></td>
        <td><?php echo $res['Course']; ?></td>
        <td>
        <?php if($_SESSION['user']['role'] == 'admin'): ?>
            <button class="w3-btn w3-red"><a href="delete.php?id=<?php echo $res['id']; ?>">Delete</a></button>
            <button class="w3-btn w3-blue"><a href="update.php?id=<?php echo $res['id']; ?>">Update</a></button>
        <?php elseif($_SESSION['user']['role'] == 'student'): ?>
            ...
        <?php endif; ?>
        </td>
    </tr>
    

    On update & delete page add this code:

    if($_SESSION['user']['role'] != 'admin'){
        die("You're not allowed to do this action!");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算