What are best practices for handling user permissions, such as the 'Admin' status, in PHP applications?

When handling user permissions in PHP applications, it is important to properly validate and sanitize user input to prevent unauthorized access. One common practice is to assign different roles to users, such as 'Admin', 'Moderator', or 'User', and then check the user's role before allowing them to perform certain actions. This can be done by storing user roles in a database and checking the user's role against the required permission level before executing sensitive operations.

// Check if user is an admin before allowing access to sensitive operation
if($_SESSION['user_role'] !== 'Admin'){
    // Redirect user to homepage or show an error message
    header('Location: index.php');
    exit();
}

// Admin-only operation
echo "You have admin privileges to perform this action.";