How can a PHP session be used to keep a user logged in while performing multiple actions on a website?

To keep a user logged in while performing multiple actions on a website, you can use PHP sessions to store the user's login credentials and check for their validity on each page load. By starting a session when the user logs in and storing their information in session variables, you can maintain their logged-in status across multiple pages until they log out.

// Start the session
session_start();

// Check if the user is logged in
if(isset($_SESSION['user_id'])) {
    // User is logged in, perform actions here
} else {
    // Redirect to login page if user is not logged in
    header("Location: login.php");
    exit();
}