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.
Related
I've got a test suite setup on a Laravel project which uses PHPUnit. Usually the dots which indicate whether the test has passed or not are shown on the same line, however in this project each dot is separated with a new line:
I've searched through all my tests to see if something is echoing an \n or a PHP_EOL but can't see anything, I've tried different versions of PHPUnit and I've tried starting over with a totally new test suite. None of these attempts have fixed the problem! The configuration in phpunit.xml for this test suite is identical to other projects too.
Can anyone suggest anything else I could do to fix this?
For me it was a space before the opening <?php tag in one of my files.
The output can come from the actual php files being tested. Aside from obvious echo sentences that can be executed during the test, it can be caused by files with the closing ?> tag that have a newline character afterwards. If this is the case I'd suggest getting rid of the closing ?> tags as they bring nothing useful.
I've been struggling with this problem for a long time now, but I cannot really find the solution. The problem is that < !DOCTYPE html etc... does not start at the first line, but leaves four blank lines before it starts.
All my files (header.php, index.php etc) have no line breaks before they start.
Anyone with any similar problems/experiences out there? It would have been of huge help!
See here for reference: view-source:http://2famous.tv/
Thank you
This is most often not caused by leading but by trailing whitespace. Lots of old PHP code still closes down code at the end, which then all too often has a stray newline:
<?php
// Lot of source code
?> <----- and a newline here which is the culprit!
To avoid this issue, never close files with ?> - PHP doesn't need it and will just stop parsing at EOF, thus implicitly avoid this 'garbage' in the output.
As for finding the files causing it - good luck, I'd start with combing any custom extensions for this and just removing all ?> markers that you can find.
As an alternative, you can probably 'fix' it by adding a single ob_start() call to your index.php, and then in the template containing the doctype executing ob_end_clean() - this puts all intermediate output in the output buffers, and then trashes it.
<!DOCTYPE html>
This is the code.
How can I fix that?
I tested the HTML/CSS/JavaScript before integrating the code with Laravel.
Make sure your PHP files don't have the closing tags (?>). They might add whitespace to your HTML.
For more info, see the PHP docs:
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
You might also want to take a look at this post: Why would one omit the close tag?
I know this is few years late but for other people.
change the page encoding to UTF-8 without BOM and it will be solved.
I have a page written with php where, for some reason, all of the plain html content of the file index.php goes on one line (look at the source) The white space is preserved, but all the new-lines disappear.
I cannot come up with any reason why this would happen, short of a syntax error, but I went through with a fine toothed comb, and found nothing out of place. This only happens on the index.php page.
Anyone have any Ideas what I should be looking for? I can post more code if necessary.
<?php
//...
include('ssi/header.php');
?>
<div>
<section id="charters">
<h2>Tanker Chartering</h2>
<!-- ... -->
The above code evaluates to something like this:
<div> <section id="charters"> <h2><a href="charters.php">Tanker ...
Maybe you have linux server and you're using windows system. Different operating systems use different new line characters. Also, for one server my FTP client uploaded it with wrong formatting, and missed every line break.
Also applications like
Notepad++ gives you the ability to change formatting and linebreaks.
It's probably the encoding of the file combined with the transfer mode on the ftp from which you downloaded/uploaded the file. Try using something like notepad2, and saving the file in UTF-8 rather than ANSI. Also upload/download with your FTP program in binary not ASCII. That stopped all of my newline issues with PHP.
could it be that your hosting provider is doing some kind of minimization for you? no newlines means less characters pused down the wire.
I've got a cakephp application that works on many other machines, but when I installed it on one particular Ubuntu/Apache2/PHP 5.3.3 machine, every file I download has an extra newline inserted at the beginning of the file.
My images won't display on my browser from this server, and when I right-click>>>save image as, the image will be saved with an extra newline at the start. This saved image is not displayable on my local machine, but as soon as I remove that extra newline, the image displays fine.
The same thing happens for text files downloaded from this application (the application allows users to upload and download files, such as these text files and images). All stored images on the server are correct, I can copy them over to my local machine and they display correctly, so the newline is somehow added from my application in rendering.
I made a simple test script (stolen from php.net) that works on this same server as a stand-alone php script:
<?php
ob_clean();
header("Content-type: image/gif");
$im = imagecreate (100, 50);
imagegif($im);
imagedestroy($im);
?>
When I use this script included inside my application at the point where I would normally display images, I get the same behavior: that is the image isn't displayed, but when I right-click>>>save image as, I can download the image, remove the newline, and correctly display the black box gif generated.
I even thought the ob_clean() call would remove any previous data in the buffers, but it did not (or I should say it didn't fix my problem).
Any ideas on how to debug this?
Have you tried running a comparison on <?php echo phpinfo(); ?> among the various servers? You may be running different versions / extensions? Maybe there is something different between them that inserts a NULL when it should be running a function or something.
Thanks for the suggestions.
It turned out that there was an extra newline at the end of a php config file for a custom application in cake, that was "include"-ed. The newline occurred after the closing "?>" in the script, and was emitted as output.
ob_clean();
Didn't remove the newline for some reason. Before I had figured out exactly where this occurred, I had found a higher level include that was calling into the code that caused the newline to be printed. When I surrounded this include like so:
ob_start();
include(whatever);
ob_end_clean();
it did prevent the newline from displaying. Only later on I found the exact place where the newline was.
I know this is 7 years old now, but I'd like to share an easy way to find the problematic file (under unix), assuming the problem is caused by a newline after a ?> closed php tag:
pcregrep -r -M --include='\.php$' '\?>\n' root/of/the/project
pcregrep is a grep varian that allows for multiline matching, so with that command I'm listing all .php files with a newline after the closing tag.