Home  >  Article  >  Backend Development  >  How to use PHP to develop permission management functions for employee attendance data?

How to use PHP to develop permission management functions for employee attendance data?

PHPz
PHPzOriginal
2023-09-25 10:52:46699browse

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.

  1. Design database structure
    First, we need to design a database structure suitable for employee attendance data. We can create two tables: user table and permission table. The user table is used to store employee information, including usernames and passwords. The permission table is used to store employee permission information, such as whether they have permissions to view, edit, delete, etc.

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) )

  1. Create login function
    Next, we need to create a login function so that employees can enter their username and password to log in to the system. During the login process, we need to verify that the username and password entered by the user are correct.

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 "用户名不存在";
}
  1. Administrator permissions setting
    Normally, only administrators have the authority to set administrator permissions . So we need to create an administrator page for the administrator to set employee permissions.

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']."'/>删除权限";
}
  1. Update permissions
    After the administrator sets the employee's permissions, we need to save the permission information into the database.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn