What are the advantages and disadvantages of using sessions for password protection in PHP?

Using sessions for password protection in PHP can be advantageous because it allows for temporary storage of user credentials during a user's session, making it easier to authenticate users without repeatedly asking for their password. However, sessions can be vulnerable to session hijacking and session fixation attacks if not implemented securely.

<?php
session_start();

// Check if user is logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true){
    // User is logged in, allow access to protected content
    echo "Welcome, ".$_SESSION['username']."!";
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}
?>