What alternative method could be used to achieve the desired redirection in the provided script?

The issue with the provided script is that the header() function is being called after output has already been sent to the browser, which will result in a "Headers already sent" error. To solve this issue, we can use output buffering to capture the output before sending any headers.

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

// Perform any necessary operations here

// Redirect to the desired page
header("Location: http://www.example.com");
exit();

ob_end_flush(); // Flush the output buffer
?>