What are the potential pitfalls when redirecting users based on SESSION variables in PHP?

Potential pitfalls when redirecting users based on SESSION variables in PHP include the risk of session hijacking or manipulation by malicious users. To mitigate this risk, it is essential to validate and sanitize the session data before using it to redirect users.

session_start();

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true){
    // Validating session data before redirecting
    $user_id = $_SESSION['user_id']; // Assuming user_id is set in the session
    // Perform additional validation if needed

    // Redirect authenticated users to a specific page
    header('Location: dashboard.php');
    exit;
} else {
    // Redirect unauthenticated users to the login page
    header('Location: login.php');
    exit;
}