PHP Header issue with ob_start() and ob_end_flush()

前端 未结 5 1096
醉话见心
醉话见心 2020-12-21 12:56

I get header problem while I use ob_start() in the beginning of a page and ob_end_flush() at the end. Because I use header function after some quer

相关标签:
5条回答
  • 2020-12-21 13:38

    I think the problem may be that you are trying to change the headers, after you have already sent something else to the output. Even when using buffering, I don't think this is possible. I think you need to call ob_end_clean() to discard the current buffer and write header information.

    0 讨论(0)
  • 2020-12-21 13:43

    Is there any space before the first <?php?

    Is there an UTF8-BOM at the beginning of the file?

    0 讨论(0)
  • 2020-12-21 13:49

    I'm a bit baffled the warning message doesn't include the location of the code that caused the first content to be sent to the client. The function headers_sent() can return that location, too. So, for debugging purposes, please try

    if($delHourExist)
    {
      if ( headers_sent($path, $lineno) ) {
        echo '<pre>Debug: output started at ', $path, ':', $lineno, "</pre>\n";
      }
      header("location: edit_delivery_hours.php");
    }
    
    0 讨论(0)
  • 2020-12-21 13:50

    There's a lot of invisible output in your code:

    <?php ob_start();?> --- THERE IS A LINE RETURN HERE ---
    --- SPACES OR TABS ---<?php include_once("header.php"); ?> --- LINE RETURN ---
    --- AND HERE ---<?php global $db;
         ...
    

    Quit starting and ending your php tags. Just do this:

    <?php 
        ob_start();
        include_once("header.php");
        global $db;
        ...
    

    Make absolutely sure that there is no output, and no whitespace outside of your tags before the call to ob_start(). If your error is on line 9, you've got a bunch of lines before that call that could be the problem. You may want to post all of those lines, numbered, so we can look at them carefully.

    0 讨论(0)
  • I resolve mi problem with some white spaces in my script with ob_start(); ob_end_flush(); and ob_end_clean(); So you could test your code

    <?php
    ob_start();
    include_once("header.php");
    
    global $db;
    $countstmt="SELECT COUNT(*) FROM tbl_lib_hours dh WHERE book_id IN(SELECT book_id FROM tbl_book WHERE user_id=".$_SESSION['uid'].")";
    $delHourExist=$db->query($countstmt);  
    if($delHourExist)
    {
    ob_end_flush();
    ob_end_clean();
    header("location:edit_delivery_hours.php");
    }
    include_once('footer.php');
    ?>
    

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