What are the limitations of using header() function in PHP for redirection after initiating a download?

The limitation of using the header() function for redirection after initiating a download is that headers must be sent before any output is sent to the browser. If you try to use header() after sending any output, it will result in an error. To solve this issue, you can use output buffering to capture the output before sending headers.

<?php
ob_start(); // Start output buffering

// Your download code here

ob_end_clean(); // Clean the output buffer without sending it to the browser

header('Location: http://example.com/redirected_page.php');
exit;
?>