I have a website with a white space problem. I am including a few files before the doctype and this is causing a white space to be outputted.
After searching I have
OK, these steps should fix your problem:
Go through all PHP files on your site. If the file ends with ?>
(and possibly some whitespace), remove the ?>
. (Of course, if there's some HTML text after the ?>
then you shouldn't remove it.)
While doing the above, also check that there's no whitespace in front of the first <?php
in any files.
If you still have problems after that, check for invisible characters in front of the <?php
. There are several ways to do that, but looking at a hex dump of the files is one good way. The first two bytes of each PHP file (that doesn't begin with HTML text) should be <?
(hex 3c 3f
).
In the unlikely event that those steps won't solve the problem, let us know (and preferably include a link to a page where the problem occurs, so that we can check the output ourselves).
I figured it out. You have to encode "UTF-8 without BOM" for the file you are including.
The master php file doesn't necessarily have to encode with "UTF-8 without BOM", and in fact I'd recommend that you don't if you have certain special characters (caused problems for me).
Was also answered here: PHP include causes white space at the top of the page
You don't have to remove the ?>
. Just the whitespace after it. Your code should look like this:
<?php
include ("include/session.php");
include ("include/updatestage.php");
?><!DOCTYPE HTML>....
You may be able to fix the problem like this:
<?php
include ("include/session.php");
include ("include/updatestage.php");
?><!DOCTYPE HTML>
But I have observed buggy behaviour in this respect (although not since PHP4), so the easiest solution that guarantees a fix is to put the <!DOCTYPE>
before the include - since neither included file outputs anything, and would break your document if it did.
<!DOCTYPE HTML>
<?php
include ("include/session.php");
include ("include/updatestage.php");
?>
<!-- Rest of HTML -->
Or this:
<?php
echo "<!DOCTYPE HTML>\n";
include ("include/session.php");
include ("include/updatestage.php");
?>
<!-- Rest of HTML -->
Remember to make sure that the <
in the opening <?php
tag is the first character in all files if you go with the second option.
EDIT Having in the first place completely failed to take into account the session/cookies problem that has reared it's ugly head, here is a working solution:
<?php
ob_start();
echo "<!DOCTYPE HTML>\n";
include ("include/session.php");
include ("include/updatestage.php");
ob_end_flush();
?>
<!-- Rest of HTML -->
Open file in Notepadd++ From top bar: Encoding->Encode in UTF8 without BOM Save the file In Dreamweaver
Press CTRL + J or go to Change->Page Settings->Tile/Encoding and unchek UNICODE BOM if it is cheked.