PHP include() before doctype, causing a white space

前端 未结 5 1951
無奈伤痛
無奈伤痛 2021-01-20 13:37

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

相关标签:
5条回答
  • 2021-01-20 14:14

    OK, these steps should fix your problem:

    1. 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.)

    2. While doing the above, also check that there's no whitespace in front of the first <?php in any files.

    3. 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).

    0 讨论(0)
  • 2021-01-20 14:16

    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

    0 讨论(0)
  • 2021-01-20 14:22

    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>....
    
    0 讨论(0)
  • 2021-01-20 14:29

    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 -->
    
    0 讨论(0)
  • 2021-01-20 14:33

    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.

    0 讨论(0)
提交回复
热议问题