Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

URGENT: How to debug headers already sent php/server error

Options
  • 28-02-2010 11:58pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Hi There,

    I need to solve this problem tonight, if anybody could help me i'd really appreciate it.

    Basically i've wrtten a CodeIgniter app for outputting PDF files using dompdf. It works fine on my local server but won't work on my webserver.

    I'm getting the error "session headers already sent". Now, I know how this error is caused but i'm having trouble finding the source of the error.

    Is there a way that I can debug the application to see what text is being echoed before the header() functions are called in dompdf?


Comments

  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    I would say it's 99% a problem with the format you are saving your files in. Some editors add what is called a BOM to the file which is ignored by most but PHP interprets it as output. It is also invisible so difficult to remove.

    What editor are you using? Do a google on "remove BOM"

    http://tomasz.sterna.tv/2009/06/php-cannot-modify-header-information-and-the-dreaded-bom/


  • Registered Users Posts: 2,234 ✭✭✭techguy


    i'm using notepad++..

    I'll check BOM now..


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Actually i'm using VS.PHP plugin in Visual studio at the moment..


  • Registered Users Posts: 3,184 ✭✭✭Kenno90


    I had that problem last year.The solution for me was there was extra spaces after the <?php and ?> tags.

    Wouldn't hurt to try


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Ok, I just tried that.. no go!!

    So, i've tried everything and i'm not even sure i'm doing it right.

    There's loads of files and I can't go trying them all. I've spent over an hour on this now.

    What am I to do now??


  • Advertisement
  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    Can you link me to the file? What is the exact error you get? Does it give a line number?

    Aside from the file format and rogue whitespace outside of <?php ?> as Kenno90 said I can't see an easy way around this...

    The fact it is ok on your dev server and not live led me to believe it was an issue with file format.

    One last quick fix might be your error handling. Perhaps your live server has a different error setting and it is outputting an error prior to the headers being sent? Try to supress errors to at least see if that is the problem.

    Insert at top of page (before any includes)
    ini_set("display_errors", 0);
    


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Ok, i just tried that ocallagh and it doesn't seem to change anything.

    I have found the line that produces the error in the DOMPDF code. It is basically
    if  ( headers_sent())
          die("Unable to stream pdf: headers already sent");
    

    I don't think DOMPDF is the problem here. I just went and made a simple PHP script to implement that on it's own (away from my CodeIgniter App) and it worked.

    This leads me to believe that my CodeIgniter app has an error in it somewhere. But I don't know how to find/fix it.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    You could use (top of script before any includes etc)
    ob_start();
    
    and then place (just before that code you quoted above)
    $output = ob_get_clean();
    
    . This will stop anything from getting sent to the browser.. and you could even inspect the contents of $output to see what is getting sent. That's just a hack and if you are using a framework like CI I don't know if it will cause issues. Also I don't know if this will stop that BOM problem


    perhaps stick a die(); function just before that code you quoted and see what the output is at that stage. I would guess it's a white space character or nothing..


  • Registered Users Posts: 2,234 ✭✭✭techguy


    OK,

    This hasn't been fixed.

    ... :(


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Hey ocallagh, that hack worked.. Thanks A LOT!!!!

    Now to find out what caused it.. I'll try the die() command now. Thanks.


  • Advertisement
  • Registered Users Posts: 2,234 ✭✭✭techguy


    Look at this badboy..

    *Thats selected whitespace.. 1 space to be precise. Now how in gods name do I find that.

    Some kind of program that would print out the filename and line number before each piece of text that is sent to the browser would be nice...

    I wonder if something like that exists.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    satan.gif, has me in stitches :D glad you have a hack working!

    It's a matter of inspecting each included file. I guarantee it's a white space character before the <?php at the top or bottom of some random file.. or else that BOM issue.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    ocallagh wrote: »
    satan.gif, has me in stitches :D glad you have a hack working!

    It's a matter of inspecting each included file. I guarantee it's a white space character before the <?php at the top or bottom of some random file.. or else that BOM issue.

    Yea you see the problem is that i'm using a framework so it's all about including files and including more files..

    CI doesn't use closing tags and i'm pretty sure i've checked all the opening tags for spaces..

    Must BOM. I must get/make a program to find these bad guys..

    Thanks for all your help.


Advertisement