What are the potential pitfalls of using HTTP Location headers for redirecting users based on their login credentials in PHP?

Using HTTP Location headers for redirecting users based on their login credentials in PHP can expose sensitive information in the URL, making it vulnerable to interception. To solve this issue, it is recommended to use server-side sessions to store user login information securely.

<?php
session_start();

if ($user_authenticated) {
    $_SESSION['user_id'] = $user_id;
    header('Location: dashboard.php');
    exit;
} else {
    header('Location: login.php');
    exit;
}
?>