What are the best practices for handling session data and browser navigation in PHP to avoid unwanted browser prompts?

When handling session data and browser navigation in PHP, it's important to properly manage session variables and ensure they are unset or cleared when they are no longer needed. Additionally, using proper headers and redirects can help prevent unwanted browser prompts, such as when a user tries to navigate away from a page with unsaved data.

// Start the session
session_start();

// Check if session data exists
if(isset($_SESSION['data'])) {
    // Use session data
    $data = $_SESSION['data'];
    
    // Unset or clear session data when no longer needed
    unset($_SESSION['data']);
}

// Redirect user to another page
header("Location: another-page.php");
exit();