Current Location: Home> Latest Articles> How to Implement User Login and Permission Management with PHP Arrays

How to Implement User Login and Permission Management with PHP Arrays

M66 2025-06-19

How to Implement User Login and Permission Management with PHP Arrays

In website development, user login and permission management are essential features. These functionalities help verify user identity and control their actions on the website, ensuring the site's security. By implementing login validation and permission control, we can effectively protect sensitive data and prevent unauthorized access.

In this article, we will show you how to implement a simple user login and permission management system using PHP arrays. Through an easy-to-understand example, we will walk you through how to build these features step by step.

1. Create the User Data Table

First, we need a table to store user information. To simplify the demonstration, we will use a 2D PHP array to represent the user table. Each row represents a user, and each column represents a different user attribute, such as username, password, and role.

$users = [
    ['username' => 'admin', 'password' => 'admin123', 'role' => 'admin'],
    ['username' => 'guest', 'password' => 'guest123', 'role' => 'guest'],
    ['username' => 'user', 'password' => 'user123', 'role' => 'user']
];

2. User Login Function

Next, we will implement the user login functionality. Users log in by entering their username and password, and the system will match the input with the data in the user array. If the match is successful, the login is successful; otherwise, the login fails.

function login($username, $password, $users) {
    foreach ($users as $user) {
        if ($user['username'] === $username && $user['password'] === $password) {
            return true;
        }
    }
    return false;
}
<p>// Example usage<br>
if (login('admin', 'admin123', $users)) {<br>
echo 'Login successful!';<br>
} else {<br>
echo 'Login failed!';<br>
}<br>

In the code above, we define a `login` function that accepts three parameters: username, password, and the user array. We use a `foreach` loop to check each user to see if they match the input. If a match is found, the function returns `true`; otherwise, it returns `false`.

3. Permission Management Function

In real systems, we not only need to validate whether a user has logged in successfully, but also control their access permissions based on their role. We assign a role attribute to each user to indicate their permission level. By checking the role, we can restrict access to certain features based on the user's permissions.

function hasPermission($username, $permission, $users) {
    foreach ($users as $user) {
        if ($user['username'] === $username && $user['role'] === $permission) {
            return true;
        }
    }
    return false;
}
<p>// Example usage<br>
if (hasPermission('admin', 'admin', $users)) {<br>
echo 'You have admin privileges!';<br>
} else {<br>
echo 'You do not have admin privileges!';<br>
}<br>

In the above code, we define a `hasPermission` function that accepts three parameters: the username, the permission level, and the user array. We use a `foreach` loop to check each user and compare the username and role. If a match is found, the function returns `true`, indicating that the user has the required permission; otherwise, it returns `false`.

4. Summary

Through the examples above, we can see how to use PHP arrays to implement user login and permission management features. While this method is simple, it provides us with a clear understanding of the basic process. In real-world applications, we typically use a database to store user information and permission data, which makes it easier to manage and scale the system.

Whether you're a beginner or an experienced developer, understanding and mastering the process of user login and permission management is crucial for website security. We hope this article provides some helpful insights.