Saturday 11 July 2015

How to fix Headers already sent error in PHP?


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\ram.php:2) in E:\wamp\www\testp\public_html\ram.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