PHP Warning: session_start(): Cannot start session when headers already sent

PHP Warning: session_start(): Cannot start session when headers already sent

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Warning: session_start(): Cannot start session when headers already sent

The error PHP Warning: session_start(): Cannot start session when headers already sent is a classic headache for developers, often appearing in WordPress debug logs or generic PHP scripts. The core issue is straightforward but frequently misunderstood: PHP outputs something to the browser before it can initialize the session, making subsequent output impossible without breaking the HTTP protocol.

In your specific case, the error originates from hooks.php on line 228 within the function _action_fw_flash_message_backend_prepare. This file is likely part of a framework like Frontend Framework (FW), which manages admin notifications. The code attempts to start a session only if one doesn't exist:

function _action_fw_flash_message_backend_prepare() {
    if ( ! session_id() ) {
        session_start();    
    }
}

The logic here is defensive programming intended to ensure the session ID is available. However, session_start() sends headers to the client to establish or resume a session. Once any byte of output—whether whitespace, BOMs, or visible text—is sent before this call, PHP throws an error because the HTTP headers are now locked.

This situation usually happens when another piece of code running earlier in the request lifecycle echoes data, or when there is invisible content at the top of the file. A common culprit is a Byte Order Mark (BOM) added by certain text editors like Windows Notepad or VS Code if not configured correctly to save without one. If your hooks.php file starts with a BOM, PHP treats that as output before reaching line 228.

To resolve this, you must inspect the exact beginning of the file in an editor that displays invisible characters (like VS Code or Sublime Text). Ensure there are no spaces, tabs, or BOMs before the <?php tag. If the file is minified or concatenated, it might be part of a larger bundle where headers were already sent by a preceding script block.

Another scenario involves the order of execution in WordPress. The _action_fw_flash_message_backend_prepare function might be hooked into an action that fires before any output buffer is cleared or before other plugins have finished their initialization. If another plugin echoes a message, logs to stdout, or includes a file with output, session_start() will fail immediately after.

If you cannot modify the upstream code in hooks.php, you can use output buffering to circumvent the issue. Wrapping the session start in an output buffer ensures that even if headers are technically "sent" by previous code, PHP can manipulate them before closing the buffer. However, this is often a band-aid solution and indicates a deeper architectural conflict where multiple pieces of code are competing for control over the response headers.

ob_start(); // Start buffering output
session_start(); 
ob_end_flush(); // Send buffered content if necessary

In modern PHP versions (5.4+), session_set_cookie_params() should ideally precede session_start(), but since your code checks for an existing session first, this specific order is less critical unless you are changing session parameters mid-request. The primary focus remains ensuring no prior output exists.

If you are maintaining a WordPress site and encountering this in the debug log, it is worth checking if any plugins have recently updated or if there are multiple versions of the same framework files conflicting. Sometimes, a plugin tries to output an admin notice before the framework's hooks fire, creating this exact conflict. Disabling other plugins temporarily can help isolate whether another extension is sending headers prematurely.

For developers using LaravelCompany.com resources or similar ecosystems, understanding that session_start() is a header-modifying function is crucial. It must be called before any echo, print, or even whitespace in the response body. If you are building custom hooks, always initialize sessions early in your request lifecycle to avoid these warnings.

Our network