How To Fix Headers Already Sent Error In PHP?


We are here to fix below error in PHP
Warning: Cannot modify header information – headers already sent by (output started at E:\wamp\www\testp\public_html\test.php:2) in E:\wamp\www\testp\public_html\test.php on line 4

Question: Why this Error comes? This error comes when sending header after the output.
OR
using “header” function after the output.

Question: Give an example when this error comes?

echo "arun";
header("Location: next.php");

Question: How to fix this error?
When you are using header function to send header, make sure you have not any output before. In another words, header function must be called first in any script(if applicable). you can not send header after the output. For Example: below will work 
header("Location: next.php");
echo "arun";
Question: Is there any another way to fix this error?
Yes, you need to set the output_buffering "on" (By default it is off)


Question: How to set output_buffering on in PHP?
ini_set('output_buffering','On');

Question: How to set output_buffering on in php.ini?
output_buffering = Off
replace with
output_buffering = On

Question: How to set output_buffering on in htaccess?
open you .htacces file and add following code.
  php_value output_buffering On
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s