Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP spits a new line before any output

Tags:

php

apache

I have a VPS from 1&1 with CentOS 5.2 64-bit.

Last Tuesday I've upgraded PHP and installed ImageMagick which had to upgrade many other packages, including Apache, MySQL, Perl, etc. Lots of stuff got upgraded in the process but being a complete noob I just went with it.

Now the problem is that all the websites look like this:

1.
2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/    xhtml1/DTD/xhtml1-transitional.dtd">

(a new line on every single PHP file before output)

I even tried ob_start(); and die(trim(ob_get_clean())); with no luck. The new line continues to persist.

I briefly checked /etc/php.ini with nothing standing out.

What can I do?

like image 749
Silviu-Marian Avatar asked Feb 12 '13 05:02

Silviu-Marian


2 Answers

Shot in the dark, but check your php.ini for an auto-prepend-file value. It might be pointing to a blank default file which happens to have a line break.

like image 144
AlienWebguy Avatar answered Sep 30 '22 16:09

AlienWebguy


The most probable cause of this is some base include file having whitespace at the end of it. If any include file has any characters outsite of the <?php and ?> tags, including line breaks or spaces, then apache will write a blank line to the beginning of the response, like you're seeing.

Unfortunately, any file in your include chain could be the culprit. If you're on a shared hosting provider, then you may need to contact their support to ensure they don't have any wacky php auto-include behavior or apache directives, like AlienWebguy mentions. Here's a good Stack Overflow question about best practices for include files. (It even points out that Smarty, a 3rd party php templating plugin had spare whitespace in its include files, even in recent/current releases.)

And here's a likely looking article for how to find and kill pages with whitespace on them. If your *nix-fu is strong enough, you could grep your file structure with a regex to try and find any such culprit. Preventatively, Mario in the SO question linked posts a few php directives which can warn about it, which could be thrown into an svn commit hook to catch any such future errors.

like image 26
Patrick M Avatar answered Sep 30 '22 17:09

Patrick M