I have two pieces of code. One is a VERY long code written by someone else - fpdf. The other was written by me. Now, I have a session started ready a mysql database. I've got everything done that I want, and now it's time to call the fpdf script. But... It generates the dreaded 'headers already sent' message.

I've tried the following:
ob_end_clean();
header("Connection: close\r\n");
ob_end_flush();
flush();
and variations thereof, but to no avail. I need to clear out the header information that's already been sent before I call the fpdf script. How do I do this properly?

    Perhaps this is a case of say, a blank line or some other text being printed before this header is sent?

      daltman1967 wrote:

      I need to clear out the header information that's already been sent before I call the fpdf script. How do I do this properly?

      You can't. Once headers are sent, they're sent - you can't modify them. The only way to send new headers would be to generate an entirely separate request (e.g. refresh the page or something of that nature).

      As laserlight suggests, the best option is likely eliminating the output (or delaying it until all necessary HTTP headers have been set). IMHO, using output buffering is a cheap workaround and not really a fix for this common problem.

        😕
        I know I've sent the headers to THIS page (at least, I think so; the code opens, reads, and modifies a mysql database. I then write to a file using php. Now, following that, I'd like to call the following:

        define('FPDF_FONTPATH','font/'); 
        require_once('html2fpdf.php'); 
        $pdf = new HTML2FPDF('P', 'mm', 'Letter');
        $pdf->AddPage();
        $fp = fopen("test.html","r");
        $strContent = fread($fp, filesize("test.html"));
        fclose($fp);
        $pdf->WriteHTML($strContent);
        $pdf->Output($ft, 'D');

        ... which is the code that generates the pdf file. However, because the headers are sent, fpdf (which is called by html2fpdf) generates the error. There MUST be SOME way around this. I've tried putting the above code in a file and including it, but the headers are still sent. How do I close down the current headers session? Can I close the current window and open a new one? If so, what's the code to do it? Thanks for your help.

          daltman1967;10948147 wrote:

          I know I've sent the headers to THIS page (at least, I think so; the code opens, reads, and modifies a mysql database. I then write to a file using php.

          Nothing that you listed there will cause headers to be sent. Headers will be sent when you output something. The PHP error message tells you the exact file and line number that caused headers to be sent.

          I don't know what your code looks like at that point, but like we've been saying... simply delay or remove whatever is outputting data at that point so that headers won't be sent.

          daltman1967;10948147 wrote:

          Can I close the current window and open a new one? If so, what's the code to do it? Thanks for your help.

          Like I said before, the only way to send new headers is to initiate a new request on the client side of things, so a redirect/reload/etc. would do that. You could use a <meta> tag or Javascript to do a redirect.

            The line that the error claims is causing the problem is an image button, as follows:

            <img id="b" src="../Images/05-Teachings/Button_ReceiveJesus_1.png" width="215px" height="28px" style="z-index: 1; position: absolute; top: 126px; left: 0px" onmouseover="this.style.cursor='hand'; SetImage('b','../Images/05-Teachings/Button_ReceiveJesus_2.png'); return false;" onmouseout="SetImage('b','../Images/05-Teachings/Button_ReceiveJesus_1.png'); return false;" border="0" /></a>

            I've output the mysql info to the screen after a form post.

            So.... a javascript redirect ... to the file with the php code calling the new script? If so, I think I could do that. I'd have to use the ?= operator on it, the use the $_GET to retrieve the info.
            If this is it, please let me know. Again, lots of thanks.

              Write a Reply...