PHP warning: headers already sent in Unknown

This question already has an answer here:

  • How to fix “Headers already sent” error in PHP 11 answers

  • Turned out that it was the line

    ob_start("ob_gzhandler");
    

    that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.


    It might be a lot of things, but as the others said, it's often just a space lying around somewhere that gets outputted and then a header() command is sent which normally is fine, but not after starting to send content back (potentially just a space in this case).

    Using ob_start() stops the output from going out right away by buffering it. So it's a potential solution, or at least way to diagnose where it's coming from as zodeus said.


    One common thing that causes those lose spaces are in this scenario.

    global.php

    <?php
      $variable = 1;
      $database = 'something else';
    ?> <-- A space here
     <-- Or here
    

    index.php

    <?php
    
      require('global.php');
      $var = dosomething();
      header('Location: http://www.example.com/');
    
    ?>
    

    One way to fix that is to remove the ?> at the end of the global.php file. You don't need those, they are only useful if you start putting HTML for example after your PHP code. So you'd have:

    <?php
      $variable = 1;
      $database = 'something else';
    

    And when you do the require() , the space is not outputted before the header() .


    Just to illustrate the problems with content outputted and headers is that other common case that gives a similar error. It happens when you forget to stop the processing after a redirection using header() .

    if ($notLoggedIn) {
      header('Location: http://www.example.com/login.php');
    }
    echo 'Welcome to my website'; // this will be outputted, 
                                  // you should have an exit() 
                                  // right after the header()
    

    I think whats happening is one of the built in php functions is outputting something. I've seen this with a couple of the IMAP functions where they out put just a " " (space character) and it screws things up. You can thry tracking it down using Xdebug or the Zend debugger, but if you have neither try wrapping output buffering around some of the functions you think may be cause it.

    ob_start();
    callYourFunction();
    ob_end_clean();
    

    Do this one function at a time and when the error goes away you'll know which function is cause you the problem, then you can either file a bug report or just leave it in as a hack. But at least then you know what function is cause the issue.

    Edit: The fact that is says your output is occurring on line 0 means that it's a C level function doing the output, not any code that's been written using PHP.

    链接地址: http://www.djcxy.com/p/34680.html

    上一篇: 使用ob的PHP头部问题

    下一篇: PHP警告:头文件已经在Unknown中发送