All about PHP/MySQL

Tuesday, February 26, 2008

Solution of a common error - Headers already sent!!

Since the beginning (not too long), while working on many php scripts i faced the 'Headers already sent' error many times and i was realy got stucked with that. I passed many hours on google and visited lot of forum posts but wasnt able to detect any quick solution. Some were saying to remove the blank spaces before starting of php tag and some where enforcing to do some other checks around the scripting files. Ofcourse the solutions worked for sometime but they were really time taking. Then a friend of mine, Rajneesh from india, suggested me to use a php function ob_start() while i was again stucked with same error in a script and deadline of that project was almost there. Here is the official definition i could get from php.net regarding this really useful function.

'' This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.''

This solved my problem for ever! Here is the method of its implementation.

Write down the ob_start() in very beginning of the script like:

<?php
ob_start();

And add this at the end:

ob_end_flush();
?>

And thats it. You will no more receive the 'Headers already sent' error ever on that script even if your script is sending the headers before any output.

I hope this solution will help someone else too facing same problem with PHP.

2 Comments:

  • Hello,

    It was very help full for me .. can you explain about ob_start and ob_end_flush to me?

    Regards,
    Asif Iqbal
    Web Developer
    email: asif@brandsynario.com

    By Anonymous Anonymous, At June 11, 2008 at 10:42 AM  

  • According to PHP manual, here what it does:

    This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer....

    By Blogger Waqas, At February 5, 2009 at 11:16 PM  

Post a Comment

Subscribe to Post Comments [Atom]



<< Home