How can including a page within another page affect the use of header() for redirection in PHP scripts?

When including a page within another page in PHP, the header() function for redirection may not work as expected. This is because the header() function must be called before any output is sent to the browser, and including a page may cause output to be sent prematurely. To solve this issue, you can use output buffering to capture any output generated by the included page before sending headers.

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

// Include the page
include 'included_page.php';

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

// Now you can safely use header() for redirection
header('Location: new_page.php');
exit;
?>