How can PHP sessions be used to store not only user login status but also user roles for access control?
To store user roles for access control in PHP sessions, you can set a session variable for the user's role after they log in. This session variable can then be checked on protected pages to determine if the user has the necessary role to access the content.
// After successful login, set the user's role in a session variable
$_SESSION['user_role'] = 'admin';
// On protected pages, check the user's role before allowing access
if ($_SESSION['user_role'] !== 'admin') {
// Redirect the user or display an error message
header("Location: unauthorized.php");
exit();
}