In what scenarios should the manual addition of session ID be considered when using header redirection in PHP?

When using header redirection in PHP, manual addition of session ID should be considered when sessions are not being properly maintained during redirection. This can happen when the session ID is lost or not carried over to the redirected page. By manually adding the session ID to the URL during redirection, you ensure that the session remains active and the user's data is retained.

<?php
session_start();

// Check if session ID is set
if(isset($_COOKIE[session_name()])) {
    // Get the session ID
    $session_id = $_COOKIE[session_name()];
    
    // Redirect to another page with session ID
    header("Location: newpage.php?".session_name()."=".$session_id);
    exit();
} else {
    // Handle the case where session ID is not set
    echo "Session ID not found.";
}
?>