top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What is the cause of this warning?

+2 votes

What is the cause of this warning?Warning: Cannot modify header information - headers already sent', and what is a good practice to prevent it?

posted Sep 27, 2015 by Vrije Mani Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Your theme is printing output (text) to the browser, but then for some reason WordPress is redirecting the user (with wp_redirect) away from that page before the whole page is rendered. You can't start printing output and then redirect, or you'll get the error you see. I've fixed this in my own experience by buffering the output of the script.
In your theme's functions.php file (which gets included every time your theme's pages load), put:
//allow redirection, even if my theme starts to send output to the browser add_action('init','do_output_buffer');function do_output_buffer(){ ob_start();}

Error Message from your Apache while browsing PHP page.
PHP Warning: Cannot modify header information - headers already sent by (output ...)

There can be only one reason for this kind of error.

Possibly you are trying to change the header of the page. But, you might have printed something before changing the header. For example the below code will give the error to you.

//See you output to browser before printing header
echo "hello... php...";
// printing your header
header("Location: index.php");

There are lots of hosting where you'll not see this kinds of error. Cause probably they have some configuration to handle this issue. But still its a good practice to not follow dirty coding like above.

Not only the Location header, it can be anything. What you can do, if you are sure, then just print all the headers at the top of the php script. This will help you to avoid the error. For example

// printing your header
header("Location: index.php");
//See you output to browser is after printing header
echo "hello... php...";

But the problem is, there can be lots if if and else conditions inside your php script. Depending upon the if else blocks, you need to change the http headers. That case above solution won't work. What you can do is, just use the output buffering technique.

Put the below code at the top of your PHP script to start the output buffering.

<?php ob_start(); ?>

And but the below code at the bottom of the php script to flush the buffer.

<?php ob_flush(); ?>

This way, whatever you are doing inside your php script, won't see this kind of weird error again.

answer Sep 30, 2015 by Manikandan J
Similar Questions
+1 vote

Getting the following two error -
Warning:session_start() : cannot send session cookie - headers already sent by(output started at.......)
Warning:session_start() : cannot send session cache limiter - headers already sent by(output started at.......)

0 votes

warning mysql_fetch_array() expects parameter 1 to be resource boolean given in

Could the experts please comment on this, and offer some advice? Thanks a lot.

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.