What are the potential pitfalls of using session variables to control access to certain features in PHP?

Using session variables to control access to features in PHP can be risky as they can be easily manipulated by users. To mitigate this risk, it is recommended to use server-side validation in addition to session variables. This means checking the user's permissions on the server side before allowing access to certain features.

session_start();

// Check if user is logged in and has appropriate permissions
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true && $_SESSION['role'] == 'admin') {
    // Allow access to admin features
    echo "Welcome, Admin!";
} else {
    // Redirect or display an error message
    echo "You do not have permission to access this feature.";
}