Forum Moderators: coopster
Hey All! A question (of course)...
I have a CMS that I use for just about everything. Homespun with the excellent help of the people of this forum. For some strange reason, when using it for one site it likes to output 9 line breaks before the doctype is written. I've tried removing this and that, looked for weird ghost characters (as best I can) and no luck.
Is there a (any) common thing(s) that result in this happening?
Some notes:
I would say the most common reason for this would be that you are somewhere including a file (config or function library) that has blank lines after the?>
Other than that, diff or beyond compare or some file/directory comparison tool would be your friend, since it seems to be working fine on some sites, but not this one.
Tom
auto_prepend_filedirectives in this particular site's .htaccess, web server, and php.ini configuration. Files automatically included via prepending behind the scenes could easily be outputting the blanks.
ergophobe is right- blank lines at the end of include()ed files are a horrible and all too common bane. They break header setting, and thus cookies & session starting. And ob_gzhandler too.
This little ditty can help track down rouge PHP files with trailing blank lines. If you're on Unix, anyhow:
find . -name '*.php' -exec perl -0777l12e 'open $in, $ARGV[0] and do { $_ = <$in>; print $ARGV[0] if m~\?>\s{2,}$~s; }' \{\} \;
What you want to do is:
- at the beginning of your script (must be after <?php, but you must also for the beginning be sure there are no extra lines before <?php), place the line:
ob_start();
(notice: nothing between the parentheses)
- then, at the point in the script where things actually start being output to the browser (including headers), or slightly before this, place the line:
ob_end_clean();
The 'ob' functions deal with buffers, or places of memory where output can be held onto before it's sent to the browser.
What ob_start() does is tells php to save all output in a buffer, and not to output anything to the browser yet - so the output of all your echo statements, etc., goes into this buffer and your visitor, for the moment, sees nothing. If there's something inside those parentheses, it will look for a function of that name and do it at the moment that the page ends, or at the moment that you have an ob_end_flush() (which tells to stop buffering, and outputs what's in the buffer to the browser), and will run that function on the contents of the buffer, and output it. ob_start('ob_gzhandler') does this - it saves all your output, and then outputs it in gzip format if the browser can handle it, or regular format if not.
ob_end_clean() just tells php to throw away the contents of the buffer, and to stop buffering - meaning, to start outputting everything from here on as normal, and not save it in any special place.
If you have a well-written script, it should be pretty obvious where output begins, so you can place ob_end_clean() in the right place. What this will do is swallow up any output that you have until ob_end_clean() is called. It's useful if you have 'mystery output' before your real output, like blank lines resulting from blank lines in include files which come before the <?php starting tag.
I would say the most common reason for this would be that you are somewhere including a file (config or function library) that has blank lines after the?>
Sheesh, I kept looking for characters above the <?php, as dreamweaver was inserting them and another program (topstyle) was the only way I could see them...
Off I go to clean things up. Thanks again!
jedit highlights php very nicely and is available for free. You might have to install a php plugin, but you can do it from inside the gui, real easy, iirc. And then when you switch to Linux, you know that same program. It also highlights your opening / closing brackets and stuff, which is pretty essential for efficient php coding. Free.
HTMLKit is real cool too, but it messes up on the php hightlighting when it goes into multiple lines. But it's got a wealth of fun HTML functions and stuff, and scads and scads of plugins. Free for non-commercial use.