How can you restrict access to certain areas of a website for different user roles in PHP?

To restrict access to certain areas of a website for different user roles in PHP, you can implement role-based access control. This involves assigning roles to users and then checking the user's role before allowing access to specific areas of the website.

// Check user role before allowing access to restricted area
$userRole = $_SESSION['user_role'];

if ($userRole == 'admin') {
    // Allow access to admin-only area
    echo "Welcome Admin!";
} elseif ($userRole == 'user') {
    // Allow access to user-only area
    echo "Welcome User!";
} else {
    // Redirect to login page if user role is not defined
    header("Location: login.php");
    exit();
}