Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

How to Fix "Headers Already Sent" error in PHP?

Headers Already Sent Error in PHP

 

It's really intimidating when you get error like this.

Most of the time you get such error when you're writing code in core programming (without frameworks).

A minute the code or the project works fine and the next you add a bit of code for new feature, it starts to show the error even in previously working feature or code.

And if I'm correct, you're in the beginner phase of programming if you're panicking or worrying after seeing such error.


In order to fix any errors, this is what I and most of the programmers do when we encounter an error.


You should consider getting an error as a good luck because it helps you to improve your debugging and research skills, which is a must have skills to be a senior developer.



Causes for this error

To understand why headers must be sent before output it's necessary to look at a typical HTTP response. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver:

Headers Already Sent Error Example

The page/output always follows the headers. PHP has to pass the headers to the webserver first. It can only do that once. After the double linebreak it can nevermore amend them.

When PHP receives the first output (printecho<html>) it will flush all collected headers. Afterward it can send all the output it wants. But sending further HTTP headers is impossible then.


How can you find out where the premature output occurred?

The header() warning contains all relevant information to locate the problem cause:

Warning: Cannot modify header information - headers already sent by (output started at /www/usr2345/htdocs/auth.php:52) in /www/usr2345/htdocs/index.php on line 100

Here "line 100" refers to the script where the header() invocation failed.

The "output started at" note within the parenthesis is more significant. It denominates the source of previous output. In this example, it's auth.php and line 52. That's where you had to look for premature output.



Typical Causes:

This error message gets triggered when anything is sent before you send HTTP headers (with setcookie or header). Common reasons for outputting something before the HTTP headers are:

1. Accidental whitespace, often at the beginning or end of files, like this:

   <?php

// Note the space before "<?php"

?>  // There should not be blank spaces here 


2. Explicit output, such as calls to echo, printf, readfile, passthru, code before <? etc.

3. A warning outputted by php, if the display_errors php.ini property is set. Instead of crashing on a programmer mistake, php silently fixes the error and emits a warning. While you can modify the display_errors or error_reporting configurations, you should rather fix the problem.

Common reasons are accesses to undefined elements of an array (such as $_POST['input'] without using empty or isset to test whether the input is set), or using an undefined constant instead of a string literal (as in $_POST[input], note the missing quotes).



Possible solutions to the problem

1. Remove all blank spaces before and after PHP code.


2. Turning on output buffering should make the problem go away; 

all output after the call to ob_start is buffered in memory until you release the buffer, e.g. with ob_end_flush.

<?php

  ob_start();


  // code 


 ob_end_flush();

?> 

This will turn output buffering on and your headers will be created after the page is buffered.

However, while output buffering avoids the issues, you should really determine why your application outputs an HTTP body before the HTTP header. That'd be like taking a phone call and discussing your day and the weather before telling the caller that he's got the wrong number.


3. After header(...); use exit;  And use 301 or 302 reference

header("location: http://example.com",  true,  301 );  exit;


Click Here to learn more about 301 and 302 redirection


4. Use JavaScript Redirection (Only if you're getting error with PHP redirection)

Instead of Below Line

//header("Location:".ADMIN_URL."/index.php");

Write

echo("<script>location.href = '".ADMIN_URL."/index.php?msg=$msg';</script>");

Or

<script><?php echo("location.href = '".ADMIN_URL."/index.php?msg=$msg';");?></script>


That's it.

Hope it helps to fix your error.

Best Wishes!


Read this article once if you want to be a BETTER debugger.


Comments