What are the best practices for utilizing the header Location function in PHP for automatic redirection?

When utilizing the header Location function in PHP for automatic redirection, it is important to ensure that there is no output sent to the browser before the header function is called. This is because the header function must be called before any actual output is sent, otherwise it will result in a "headers already sent" error. To avoid this issue, it is recommended to place the header function at the very beginning of the PHP script.

<?php
// Ensure no output is sent before calling the header function
ob_start();

// Perform any necessary logic

// Redirect to a new page after 3 seconds
header("Refresh: 3; url=redirected_page.php");

// Clear output buffer and send the header
ob_end_flush();