开发者

PHP Header issue with ob_start() and ob_end_flush()

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 query execution.

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

 $countstmt="SELECT COUNT(*) FROM tbl_lib_hours dh WHERE book_id IN(SELECT book_id F开发者_StackOverflow中文版ROM tbl_book WHERE user_id=".$_SESSION['uid'].") ";       
 $delHourExist=$db->query($countstmt);  
 if($delHourExist){
      header("location:edit_delivery_hours.php");
 }
 ....
include_once('footer.php');
ob_end_flush();

In header.php there I also added ob_start(); and in footer.php i added ob_end_flush(); , but I think that is not problem, although other pages are running with same script I write above

The error I am getting:

Warning: Cannot modify header information - headers already sent in D:\xampp\htdocs\project\add_book_hours.php on line 9


Is there any space before the first <?php?

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


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.


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");
}


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜