Decoding the "Headers Already Sent" Error and Fixing Logic Flow
The PHP warning Warning: Cannot modify header information - headers already sent by is a classic runtime error that occurs when you attempt to send HTTP headers after output has already begun. This usually stems from a misunderstanding of how the HTTP protocol interacts with PHP's execution model.
HTTP headers are metadata about the responseâthings like Location, Content-Type, or custom authentication tokens. These must be transmitted immediately upon receiving the first byte of data. Once your script prints anything to the output buffer, even invisible characters, the headers are considered "sent." At that point, trying to modify them causes PHP to throw a fatal error because the HTTP transaction is effectively locked in its current state.
This specific issue often trips up developers when using include or require statements for layout files like navigation bars or footers. Consider a scenario where you have a header file (header.php) containing a <body> tag with menu links. If that file executes before your logic attempts to redirect the user, you will encounter this exact error.
Looking at the provided code snippet, the root cause is clear: the include_once 'header.php'; statement sits right at the top of the script. Regardless of what happens inside that included file, PHP executes it first. Even if header.php starts with whitespace or a blank line, any HTML content within it counts as output. Consequently, when your logic later runs and tries to execute header("Location:summary.php");, PHP halts execution because the headers were already flushed by the previous include.
The most intuitive fix is to rearrange the code so that header operations happen before any output-generating statements. Moving the include statement below the header() call resolves the warning, but it introduces a significant functional trade-off: you lose the menu options for each location. If your header.php file contains navigation logic that depends on user context, moving it after the redirect logic might render those menus irrelevant because the user has already been redirected or because the context needed to display them hasn't been established yet.
To solve this without sacrificing functionality, you need to rethink how your header is included. Instead of blindly including a full HTML structure at the top of every page, isolate the output-sensitive logic from the structural layout. You can use conditional includes or specific checks to ensure headers are only sent when necessary and in the correct order.
One effective pattern involves separating concerns between layout and logic. Place your header.php inside a dedicated layout file that is included after all business logic has determined the appropriate redirect or response status. However, be cautious: if you include the header too late, you might miss the opportunity to set headers that are strictly required before any HTML is generated.
If your application requires dynamic headers based on user input (like authentication tokens), ensure those header() calls occur before any echo statements or includes that output content. If you must include a layout file early, consider using Output Buffering Functions like ob_start(). By starting an output buffer at the very beginning of the script, PHP stores all output in memory instead of sending it immediately. This gives you a window to set headers later, even after output has technically started.
However, relying on buffering is not always the silver bullet. It adds complexity and can mask underlying architectural issues. The preferred approach remains keeping logic clean: ensure your header() calls happen before any include that outputs HTML. If your header.php file needs to display menus based on user roles or session data, make sure those checks happen before the include statement in your main script.
Ultimately, the error is a symptom of execution order, not a bug in PHP itself. By restructuring your script to prioritize header-setting logic over layout inclusion, you maintain HTTP protocol compliance while preserving the dynamic behavior of your navigation menus.