What best practices should be followed to prevent header modification errors in PHP scripts, especially in the context of admin areas and web pages?

Header modification errors in PHP scripts can occur when headers are sent before calling functions like header() or setcookie(). To prevent these errors, it's recommended to ensure that no output is sent to the browser before modifying headers. This can be achieved by placing header modification functions at the beginning of the script or using output buffering to capture any output before sending headers.

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

// Your PHP code here

// Modify headers only after all processing is done
header('Location: admin.php');

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