Problem with spaces before doctype

I had some strange problem when I have 2 copies of the same site in two different folders. One of these two instances has a space before the doctype declaration, so I am having problems working with facebook because the document is not generated correctly.

There are some php calculations before html, but no echo expressions or anything else.

What could be the reason for two authentication sites on the same server in different folders who have this problem?

+4
source share
8 answers

I am pretty sure that you have some trailing spaces at the end of PHP. 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 a note at the end to get the stackoverflow layout editor to show extra lines after closing the tag ?> . PHP automatically trims one newline after the tag is included, but if you have several, they will be interpreted as an empty space before the doctype declaration.

The easiest fix? Remove the closing tag ?> From all your inclusions. This is a valid method and may be the fastest way for you to get up and running.

+8
source

You do not say which browser you are having problems with. IE is used to check the first few letters of the page for the doctype type, and if it is not one (for example, a space), it switches to quirks mode.

+1
source

This is especially common if you use. Carefully check all of your included or other php files that appear before your exit or headers.

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

Why? It is not known, on one production server this problem constantly raises my head for me (CentOs 5), but this does not happen on my dev machine (latest version of Fedora), and I have no problems.

Honestly, maybe there is something that I could find to find out why, but since the correct use says β€œno extra spaces or lines,” I just do it and don’t worry too much about β€œwhy does this happen -differently on my servers "There is so much. (The kind of message I know)

0
source

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

Upload documents (which were included before <!DOCTYPE ) to the host using the CPanel file manager, then select 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 this is hidden empty space !! Save the document and you will notice that everything is in order now.

I do it for all my projects

0
source

In my case, this works great:

 $ 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 
0
source

I came across this when a line break was above Doctype. I tried a few things and then just put doctype in php.

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

Not sure if this is the right way, but it worked for me. If you are using the more extensive doctype type, you need to avoid special characters.

0
source

I just spent a day solving this problem, when my libraries worked fine on static pages, but on a dynamic system with a script master in the beginning (apache mod_write overwrites everything in a master script), the browser turned over to backward compatibility mode (js: alert (document. compatMode ())) and distorted my style. All of my included files were finished correctly, not echo, etc. Problem? Empty line above hole <php master script .......

0
source

Do you use session_start () or header () anywhere? then (without outbutbuffering) no spaces or other characters can be sent to the client before these functions.

-1
source

Source: https://habr.com/ru/post/1336332/


All Articles