PHP "Cannot modify header information - headers already sent" Solved!

Warning: Cannot modify header information - headers already sent by

To fix this error I have faced many challenges. You might have seen many resolutions on this, but sometime this could be very difficult to fix in existing templates. But finally I got the correct resolutions, I applied three methods out of this I was able to apply one fix that help to correct my existing template.  
But, I will explain all three methods, so that any of the method you can try to your template to fix your issue.

Suppress your error: If you are sure, the one error you are facing on your template, that is because of know function or because of known PHP page.
Then you can use below error suppress method.
Just put this function above to that function or on the top of your .php page.
Example:

Syntax:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
<?php
error_reporting(0);
// your function/page code will be here.
?>
 Note: I am recommending this only when you are aware which function throwing an error or which page is throwing an error.

Turn off PHP Output Buffeting: As this method was applicable for PHP template. So used this methods and its worked like champ. I recommend this method for your problem.

To use this method, just keep one thing in mind, you have to use this function on very first line of your PHP file. Don’t forget to use on first line. It will skip header buffeting from your error script. You will not face “Header Already sent” error anymore on your script.
Example:
<?php
ob_start()
// your function should start from hear.
Function myFunction()
{
//your code will go hear
}
?>
3. Apply if condition on Header Sent warning: It’s a simple method to solve your warning message.  check if header already sent then do not set header again. Skip your that error code
Example:


<?php
   
if (headers_sent()) {
    die("Redirect Error");
}
else{
    exit(header("Location: /myPage.php"));
}
?>


3 comments :

Post a Comment

Leave A Comment...