开发者

Problem with whitespace before doctype

I got some strange 开发者_开发问答problem where i have 2 copies of same website in 2 different folders. In one of those 2 copies has whitespace before doctype declaration so i have problems to work with facebook as document is formated incorrectly.

Before html there is some php calculations, but there is no echo statements or something.

What could be cause for 2 identique websites under same server in different folders, one having this issue?


I'm almost positively certain that you have some trailing whitespace at the end of a PHP include. Check there first.

Example:

<?php
    // header file
    include 'other_file.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"><!-- etc.  -->

Then

<?php
    // other_file.php
    // connects to database, or something,
    // but notice the whitespace after the closing tag
?>



... pretend this text isn't here

I added the note at the end to get stackoverflow's markdown editor to show the additional lines after the closing ?> tag. PHP automatically truncates one newline character after the tag in includes, but if you have more than one they will be interpreted as blank space before your doctype declaration.

Easiest fix? Remove the closing tag ?> from all of your includes. This is a valid technique, and could be the fastest way for you to get up and running.


You don't say which browser you're having problems with. IE used to check the first few letters of the page for a doctype and, if it isn't one (such as whitespace), it would go into quirks mode.


This is especially common if you are using includes. Check over all your includes or other php files carefully that come before your output or headers.

<?
   echo "php here"; 
?>

This will cause a problem if it's in an includes
that comes before your headers or doctypes. 
Remove any non php here, file should end with "?>"
not whitespace, linefeeds or characters.

As for why? Unknown, on one production server this issue raises it's head constantly for me (CentOs 5) but on my dev machine (latest Fedora) it doesn't happen and I have no issues.

Honestly there's probably something I could track down to find out why, but since proper usage says "no extra spaces or lines" I just do that and don't worry too much about the "why is it handled differently on my servers" all that much. (Kind of a cop-out I know)


I ran into this where a line break was above the Doctype. I tried a few things and then ended up just placing the doctype in the php.

<?php
echo '<!DOCTYPE html>';
... other php code/includes/etc ...
?>
<html>
... other html elements ...

Not sure if that's the right way to go about it, but it worked for me. If you're using a more extensive doctype, you'll need to escape special characters.


I just spent a day solving this problem where my libraries worked fine on static pages but under a dynamic system with a master script at the start (apache mod_write rewriting all to master script) the browser flipped into backward compat mode (js: alert(document.compatMode()) ) and distorted my styling. All my include files were ended properly and no echos, etc.. The problem? A blank line above the opening <?php of the master script.......


You may not believe but I have a MAGIC way to solve this problem:

Upload your documents (which are included before <!DOCTYPE) on your host using File Manager of CPanel, then choose the document and Click "Edit" to see the source code of the document. Then press Ctrl+Home then press Delete!

You see that nothing will be deleted but that hidden white space!! Save your document and you will notice that everything is OK now.

I do this for all of my projects


In my case this works fine:

$ brew update
$ brew install coreutils
$ cd directoryWithSymfony2Project
$ for file in $(find ./ -name '*.twig' -or -name '*.php'); do sed `echo -e 's/[\xC2\xA0]//g'` $file > temp; echo 'Processing ' $file;rm $file; mv temp $file; done 


I had the same issue, and the culprit was a whitespace at the top of one of my included PHP files, before <?php:


<?php //Line above this line was the problem

function gggl_vertical_cards(){
  ob_start();
  ?>
//...


do you use session_start() or header() anywhere? then (without outbutbuffering) no whitespace or other character is allowed to send to the client before this functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜