How to use the Layui framework to develop a permission management system that supports multi-user login
Introduction:
In the modern Internet era, more and more applications It is necessary to support multi-user login to achieve personalized functions and permission management. In order to protect the security of the system and the privacy of data, developers need to use certain means to implement multi-user login and permission management functions. This article will introduce how to use the Layui framework to develop a permission management system that supports multi-user login, and give specific code examples.
We can design a user table and a role table to store user information and role information. The user table contains user ID, user name, password and other fields; the role table contains role ID, role name and other fields. In addition, we can also design a permission table to store permission information for various functions and pages. The permission table includes fields such as permission ID, permission name, and permission URL.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" href="layui/css/layui.css"> </head> <body> <div class="layui-container"> <form class="layui-form" action="login.php" method="post"> <div class="layui-form-item"> <label class="layui-form-label">用户名</label> <div class="layui-input-inline"> <input type="text" name="username" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密码</label> <div class="layui-input-inline"> <input type="password" name="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="login-btn">登录</button> </div> </div> </form> </div> <script src="layui/layui.js"></script> <script> layui.use(['form'], function() { var form = layui.form; form.on('submit(login-btn)', function(data) { // 处理登录请求 }); }); </script> </body> </html>
First, we need to write a login.php script to handle login requests. In this script, we can verify the correctness of the username and password by querying the user table, and return the query results to the front-end page. The following is a sample code of a simplified login.php script:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "123456", "mydb"); if (!$conn) { die("连接数据库失败:" . mysqli_connect_error()); } // 处理登录请求 $username = $_POST["username"]; $password = $_POST["password"]; $sql = "SELECT * FROM user WHERE username='" . $username . "' AND password='" . $password . "'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // 登录成功,设置登录状态和权限 session_start(); $_SESSION["username"] = $username; // 获取用户角色 $role = mysqli_fetch_assoc($result)["role"]; // 根据角色设置权限 if ($role == "admin") { // 设置管理员权限 $_SESSION["role"] = "admin"; } else if ($role == "user") { // 设置普通用户权限 $_SESSION["role"] = "user"; } else { // 设置访客权限 $_SESSION["role"] = "guest"; } echo "success"; } else { // 登录失败 echo "error"; } // 关闭数据库连接 mysqli_close($conn); ?>
After completing the login verification, we can decide which functions and pages the user can access based on the user's permissions. When creating each function and page, we can use PHP to determine the user's permissions and perform corresponding processing based on the judgment results.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>权限管理</title> <link rel="stylesheet" href="layui/css/layui.css"> </head> <body> <div class="layui-container"> <table class="layui-table" lay-data="{url:'get_permissions.php', page:true, limit:10, id:'permission_table'}" lay-filter="permission_table"> <thead> <tr> <th lay-data="{field:'id', width:80, sort:true}">ID</th> <th lay-data="{field:'name', width:120}">权限名</th> <th lay-data="{field:'url'}">URL</th> </tr> </thead> </table> </div> <script src="layui/layui.js"></script> <script> layui.use(['table'], function() { var table = layui.table; // 监听表格事件 table.on('tool(permission_table)', function(obj) { var data = obj.data; if (obj.event === 'edit') { // 编辑权限 editPermission(data); } else if (obj.event === 'delete') { // 删除权限 deletePermission(data); } }); // 编辑权限 function editPermission(data) { // TODO: 编辑权限的逻辑 } // 删除权限 function deletePermission(data) { // TODO: 删除权限的逻辑 } }); </script> </body> </html>
In the permission management page, we can listen to the events of the table and trigger the corresponding event handler when the user clicks the edit or delete button. . In the event handling function, we can use AJAX technology to send the corresponding operation to the background for processing.
The above is the detailed content of How to use the Layui framework to develop a permission management system that supports multi-user login. For more information, please follow other related articles on the PHP Chinese website!