Home >Backend Development >PHP Tutorial >PHP implements permission authentication and authorization

PHP implements permission authentication and authorization

PHPz
PHPzOriginal
2023-06-22 10:38:361704browse

PHP is a programming language widely used in web development. In web applications, security is crucial, with permission authentication and authorization being one of the important aspects of protecting web applications from unauthorized access. In this article, we will learn how PHP implements permission authentication and authorization.

Permission authentication is to verify whether a specific user has the right to access resources or perform operations. In order to implement this function, a user system is first needed, including user authentication and confirming whether the user has permission to access specific resources. Authentication can be done using usernames and passwords, while authorization can be done using access control lists (ACLs) and roles to determine whether a user has permission to access a specific resource.

A common way to implement user authentication and authorization in PHP is to use sessions. We can create a session after the user logs in and check whether the session exists and whether the user is authorized on each page visit.

The following is a sample code to implement user authentication and authorization using PHP:

// 验证用户身份
$username = $_POST['username'];
$password = $_POST['password'];

if ($username === 'admin' && $password === '123456') {
    session_start();
    $_SESSION['user'] = 'admin';
}

// 检查用户是否被授权访问页面
session_start();
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
    header('Location: /login.php');
    exit();
}

In the above code, we first get the username and password from the user's login form, and then verify these Are the credentials correct? If the authentication is successful, we create a session variable named "user" and set it to "admin".

Next, we check if the session exists and if the user is authorized to access the page. If the user is not authenticated or authorized, redirect them to the login page.

In the real world, user systems will be more complex and may involve many roles and permissions. In this case, we can use a database to store user data and permission information, and use PHP and SQL queries to retrieve and update this information.

The following is a sample code for implementing database-based authentication and authorization using PHP and MySQL:

// 验证用户身份
$username = $_POST['username'];
$password = $_POST['password'];

$conn = new mysqli('localhost', 'root', 'password', 'example');

$result = $conn->query("SELECT * FROM users WHERE username = '{$username}' AND password = '{$password}'");

if ($result->num_rows === 1) {
    session_start();
    $_SESSION['user'] = $username;
}

// 检查用户是否被授权访问页面
session_start();
if (!isset($_SESSION['user'])) {
    header('Location: /login.php');
    exit();
}

$role = $_SESSION['user'];

$result = $conn->query("SELECT * FROM roles WHERE role = '{$role}'");

if ($result->num_rows === 1) {
    $permissions = $result->fetch_assoc()['permissions'];
    $permissions = explode(',', $permissions);
}

if (!in_array('page1', $permissions)) {
    header('Location: /403.php');
    exit();
}

In the above code, we first get the username and password from the user's login form , and then use a MySQL query to verify that these credentials are correct. If the authentication is successful, we create a session variable called "user" and set it to the username.

Next, we check if the session exists and get the role of the user. We then query the list of permissions used for the role and split it into an array.

Finally, we check if the user is authorized to access the specific page. If the user is not authenticated or authorized, redirect them to the login page or a 403 error page.

In short, implementing permission authentication and authorization in PHP requires three steps: user authentication, user authorization and permission checking. The methods for implementing these steps vary from project to project, but sessions and databases are usually among the most important tools. When implementing permission authentication and authorization, we should always consider security and ensure that users can only access pages and resources for which they are authorized.

The above is the detailed content of PHP implements permission authentication and authorization. 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