What are some common methods to restrict user access in PHP applications?

One common method to restrict user access in PHP applications is by implementing role-based access control (RBAC). This involves assigning roles to users and defining permissions for each role. By checking the user's role and permissions before allowing access to certain parts of the application, you can control what actions they are allowed to perform.

// Example of role-based access control in PHP

// Define roles and their corresponding permissions
$roles = [
    'admin' => ['create', 'read', 'update', 'delete'],
    'editor' => ['create', 'read', 'update'],
    'viewer' => ['read'],
];

// Check if user has permission to access a certain page
function checkPermission($role, $permission) {
    global $roles;
    
    if (isset($roles[$role]) && in_array($permission, $roles[$role])) {
        return true;
    } else {
        return false;
    }
}

// Example usage
$userRole = 'admin';
$requestedPermission = 'delete';

if (checkPermission($userRole, $requestedPermission)) {
    echo 'Access granted!';
} else {
    echo 'Access denied!';
}