2
votes

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 FROM 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

5
what is the problem you are experiencing?Anthony Forloney
What's the 'header' problem? Also, I don't think it's a good thing to nest ob_start calls.zneak
Please read this question on preventing SQL Injection. It's not related to your current problem, but it's very useful information. stackoverflow.com/questions/60174/…Kibbee
Doesn't the warning message contain two locations a) the file and line that caused the "first" content output and b) the file and line that caused the warning? Something like PHP Warning: Cannot modify header information - headers already sent by (output started at file1:linenumber1) in file2 on line linenumber2VolkerK
Can you add line 9 of add_book_hours.php to your question?Karsten

5 Answers

3
votes

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

Is there any space before the first <?php?

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

4
votes

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
votes

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
votes

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');
?>