How can a PHP function be used to manage access control for specific pages based on user groups?

To manage access control for specific pages based on user groups in PHP, you can create a function that checks the user's group and redirects them accordingly. This function can be called at the beginning of each protected page to ensure that only authorized users can access it.

<?php
session_start();

function checkAccess($allowedGroups) {
    if (!isset($_SESSION['user']) || !in_array($_SESSION['user']['group'], $allowedGroups)) {
        header("Location: unauthorized.php");
        exit();
    }
}

// Example usage:
// Check if user is in 'admin' or 'editor' group to access the page
checkAccess(['admin', 'editor']);
?>