Home > Article > Backend Development > How to use PHP to develop permission management functions for employee attendance data?
How to use PHP to develop the permission management function of employee attendance data?
As an enterprise develops and expands in scale, the management of employee attendance data becomes more and more important. In order to protect the security and privacy of data, a reasonable permission management system becomes very necessary. This article will introduce how to use PHP to develop a permission management function for employee attendance data, and provide specific code examples.
The following is a simple database design example:
User table (users):
user_id(INT): User ID
username(VARCHAR): User name
password(VARCHAR): Password
Permissions table (permissions):
permission_id(INT): Permission ID
user_id(INT): User ID
view(INT): View permission (1 Represents authority, 0 represents no authority)
edit(INT): Edit authority (1 represents authority, 0 represents no authority)
delete(INT): Delete authority (1 represents authority, 0 represents no authority) )
The following is a simple login function code example:
<?php // 获取用户输入的用户名和密码 $username = $_POST['username']; $password = $_POST['password']; // 根据用户名查询用户信息 $query = "SELECT * FROM users WHERE username = '$username'"; $result = mysqli_query($conn, $query); // 验证用户输入的密码是否正确 if ($result && mysqli_num_rows($result) > 0) { $user = mysqli_fetch_assoc($result); if ($password == $user['password']) { // 登录成功,可进行相应操作 // 将用户ID存入session供后续使用 $_SESSION['user_id'] = $user['user_id']; } else { echo "密码错误"; } } else { echo "用户名不存在"; }
The following is a simple administrator page example:
<?php // 验证当前用户是否为管理员,如果不是则跳转到其他页面 if ($_SESSION['user_id'] != $admin_user_id) { header('Location: index.php'); exit(); } // 获取员工列表 $query = "SELECT * FROM users"; $result = mysqli_query($conn, $query); // 显示员工列表以及设置权限 while ($user = mysqli_fetch_assoc($result)) { echo $user['username']; echo "<input type='checkbox' name='view_permission[]' value='".$user['user_id']."'/>查看权限"; echo "<input type='checkbox' name='edit_permission[]' value='".$user['user_id']."'/>编辑权限"; echo "<input type='checkbox' name='delete_permission[]' value='".$user['user_id']."'/>删除权限"; }
The following is a simple code example for updating permissions:
<?php // 获取管理员设置的权限信息 $view_permission = $_POST['view_permission']; $edit_permission = $_POST['edit_permission']; $delete_permission = $_POST['delete_permission']; // 更新权限信息 foreach ($view_permission as $user_id) { $query = "INSERT INTO permissions (user_id, view) VALUES ('$user_id', 1) ON DUPLICATE KEY UPDATE view=1"; mysqli_query($conn, $query); } foreach ($edit_permission as $user_id) { $query = "INSERT INTO permissions (user_id, edit) VALUES ('$user_id', 1) ON DUPLICATE KEY UPDATE edit=1"; mysqli_query($conn, $query); } foreach ($delete_permission as $user_id) { $query = "INSERT INTO permissions (user_id, delete) VALUES ('$user_id', 1) ON DUPLICATE KEY UPDATE delete=1"; mysqli_query($conn, $query); }
Through the above steps, we have completed the permission management function of developing employee attendance data using PHP. When an employee logs into the system, it is judged whether he or she can perform corresponding operations based on the permissions he or she has. Administrators can set employee permissions on the administrator page and save them in the database.
Of course, the above code is just a simple example, and more security verification and optimization are required in actual development. I hope this article can be helpful to develop the permission management function of employee attendance data in PHP.
The above is the detailed content of How to use PHP to develop permission management functions for employee attendance data?. For more information, please follow other related articles on the PHP Chinese website!