Codeigniter: Message: Cannot modify header information - headers already sent by (output started at

Codeigniter: Message: Cannot modify header information - headers already sent by (output started at

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

Stefan Bogdanescu

Founder & Senior Architect

Why "Headers Already Sent" Crashes Your CodeIgniter App on Linux

You are seeing a classic PHP error when moving your application from Windows to Linux: Cannot modify header information - headers already sent by. The traceback points directly to /home/ansofcm8/public_html/apps/resources/math/application/core/MY_Controller.php at line 1, inside the Session library. This isn't a mystery; it is a definitive signal that your script has outputted something to the browser before trying to send HTTP headers like setcookie(), session_start(), or redirect responses.

In PHP, headers must be sent before any body content. Once you write a single character—whether it's an HTML tag, whitespace, or even a BOM (Byte Order Mark)—the headers are "sent." Any subsequent attempt to modify them triggers a fatal error. The fact that this works on your Windows machine but fails on Linux often points to invisible characters introduced during the upload process or differences in how the web server handles file encoding.

The Invisible Culprit: Byte Order Marks (BOM)

The most common cause for this specific discrepancy between Windows and Linux environments is a Byte Order Mark (BOM). Many text editors, particularly those running on Windows like older versions of Notepad or VS Code with certain default settings, prepend a BOM (EF BB BF) to UTF-8 files by default. This invisible character sits right at the very top of your file, before the <?php tag.

When you upload these files to a Linux server via FTP or SCP, that BOM remains intact. When PHP parses the file, it reads that first byte immediately. If your application relies on cookies or sessions (which is standard for CodeIgniter), the framework attempts to set headers during its bootstrap phase. Because the BOM was already flushed as output, PHP throws the "headers already sent" warning.

To fix this, you must ensure your source files are saved without a BOM. Open MY_Controller.php and the rest of your core files in a text editor that allows you to view hidden characters (like Notepad++, Visual Studio Code, or Sublime Text). Look for the green triangle icon next to the file name or enable "Show All Characters" to visually confirm if a BOM exists. If it does, change the encoding settings and save as UTF-8 without BOM.

Whitespace Before the Tag

While less likely than a BOM if you edited files locally on Windows, whitespace before the opening <?php tag is another frequent offender. It's easy to accidentally press Enter or add comments before the PHP opener in an editor configured for Linux-style line endings (LF).

Check the very first bytes of your files using a hex editor or a command-line tool like file on the server:

file /home/ansofcm8/public_html/apps/resources/math/application/core/MY_Controller.php

If the output mentions "with BOM" or if you see spaces before the < in a hex dump, that is your issue. Even a single space between <?php and an opening brace can cause issues in strict environments, though usually, it's content before the tag that triggers the error.

Mixed Line Endings and Hidden Files

Another subtle issue specific to cross-platform deployments is mixed line endings (CRLF vs. LF). While PHP generally handles this gracefully, some frameworks or libraries might behave unexpectedly if the file structure isn't clean. Additionally, ensure you haven't accidentally uploaded a .gitkeep or hidden configuration file that gets executed.

In your case, the error specifically originates in MY_Controller.php. If this class extends a base controller and includes other files, check those inclusion paths as well. A common pitfall is having a helper file or a view included within the controller that contains whitespace at the top. Debugging requires isolating which include statement is flushing the buffer first.

The Solution Path

The fix is almost always file hygiene. Re-upload your application using an SFTP client that preserves Unix line endings and explicitly strips BOMs if supported, or simply re-save all PHP files in your local environment as "UTF-8 without BOM" before deploying. Verify the MY_Controller.php file specifically, as that is where the error stack trace originates.

Once you clean up the output stream initialization, the session library will be able to set its cookies, and your dynamic menu CMS will function correctly on the Linux server just as it did on Windows. If you are looking for a more robust framework that handles these edge cases automatically out of the box, evaluating Laravel is a strong alternative; you can find detailed migration guides and comparison articles at https://laravelcompany.com to understand how modern frameworks manage the HTTP lifecycle differently.

Our network