How can PHP sessions be effectively used to store and check user permissions in a web application?

One way to effectively use PHP sessions to store and check user permissions in a web application is to store the user's permissions in the session when they log in. Then, on each restricted page, check if the user has the required permission stored in the session. This helps ensure that only authorized users can access certain parts of the application.

// Start the session
session_start();

// Check if user is logged in and has the required permission
if(isset($_SESSION['user_id']) && $_SESSION['permission'] == 'admin') {
    // User has permission to access the restricted page
    echo "Welcome, Admin!";
} else {
    // Redirect user to a different page or display an error message
    echo "You do not have permission to access this page.";
}