How to fix the "Warning: Cannot modify header information: headers already sent ..." error in PHP

How to fix the "Warning: Cannot modify header information: headers already sent ..." error in PHP

If you are facing a PHP error "Warning: Cannot modify header information - headers already sent by ..." this article might help you to solve this. This article aims to provide students or beginners with a reference for solving some PHP errors they are facing and to enhance their knowledge to avoid this kind of error.

This kind of PHP error is basically due to how the code is written. The error represents that we developers must write or execute the header modification before the script outputs anything otherwise the PHP will return a PHP warning that says that the "headers already sent".

What are the PHP functions that modify the HTTP header?

Here are some of the PHP function more often used that modify the HTTP headers:

  • header()
  • header_remove()
  • session_start()
  • session_regenerate_id()
  • setcookie()
  • setrawcookie()

What are the reasons for the "Headers already sent" PHP error or warning?

The "Headers already sent" can be caused by an intentional or unintentional reasons such as the following:

  • Leaving whitespace before or after the
    <?php ?>
  • Previous Error Messages or Notices occurred
  • Using the following functions:
    • echo
    • print
    • or any other PHP string function that outputs something.
  • Opening the <html> tag before modifying the headers.

Examples

Example #1: Cause by the Whitespaces

The following PHP script is an example of PHP code that can cause the Warning: Cannot modify header information - headers already sent.

  1. <?php
  2. header("Content-Type: application/json");
  3. print(json_encode(["sample_data" => 'This is a sample json data value.']));
  4. ?>

The script will return a PHP warning like the following image:

How to fix 'Warning: Cannot modify header information: headers already sent ...' error in PHP

The script can be easily solved by removing the Whitespaces before the PHP script or before the header() function.

  1. <?php
  2. header("Content-Type: application/json");
  3. print(json_encode(["sample_data" => 'This is a sample json data value.']));
  4. ?>

The script will return a PHP warning like the following image:

How to fix 'Warning: Cannot modify header information: headers already sent ...' error in PHP

Example #2: Cause by the Raw HTML tag

The example below represents the idea of PHP coding that causes a warning that says "header have been sent already".

  1. <html>
  2. <body>
  3. <?php
  4. ?>
  5. <h1>Sample</h1>
  6. </body>
  7. </html>

The following image is the result of the PHP script above:

How to fix 'Warning: Cannot modify header information: headers already sent ...' error in PHP

To solve this, we can simply transfer the session_start() function to the first line like the following.

  1. <?php
  2. ?>
  3. <html>
  4. <body>
  5. <h1>Sample</h1>
  6. </body>
  7. </html>

The following image is the result of the PHP script above:

How to fix 'Warning: Cannot modify header information: headers already sent ...' error in PHP

Furthermore, the "Headers already sent" PHP warning may not return a message to some other servers due to the setup on their PHP configuration file which means that if this warning or error occurred in your project in a certain server, you can also fix it without making any changes to your codes by updating the output_buffering value in your PHP Configuration File [i.e. php.ini ].

output_buffering=4096

That's it! I hope this article about How to fix the "Warning: Cannot modify header information: headers already sent ..." error in PHP will help you with what you are looking for.

Explore more on his website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment