tags:

views:

1023

answers:

4

i m getting header problem while i m using ob_start(); in the start of a page and ob_end_flush(); end of the page. because i m using header function after some query execution

my code is here

 <?php ob_start();?>
    <?php include_once("header.php"); ?>
    <?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);  /* i m using ez mysql class*/ output is 1

       if($delHourExist)
      {
       header("location:edit_delivery_hours.php");
      }
  -------------------
  --------------------
  --------------------
  some php codes
  --------------------
  --------------------
<?php include_once('footer.php');
<?php ob_end_flush();?>

here one thing i tell u in header.php there i also add 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

please tell me what to do?

EDIT: i m getting

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

Is there any space before the first <?php?

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

Karsten
No space,and no UTF-8 BOM( exatcly i don't know waht is this)
diEcho
The UTF-8 BOM tag is a hidden character. You'll need to look up an editor that you can use to show this (eg. Notepad++).
Blair McMillan
(would +1 but out of votes) white space and BOM are always the first thing to check.
Pekka
Don't forget about trailing newlines. There's a lot of whitespace in this code example. In most cases, you only want a single PHP block in your source files. It'll save some pain.
Pestilence
A: 

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.

Kibbee
where i have to write `ob_end_clean()`, i already written `ob_end_flush()` at the end of script
diEcho
You can totally alter headers at any point before output is performed (including buffered output generation).
Pestilence
+1  A: 

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.

Scott Saunders
there is no spaces, but tabs ans new line are there,please tell me one thing, how do make indentation the code shown by you, without tabs or new line?
diEcho
I agree with Scott: you have a lot of unnecessary `<?php` open and close block. Try to put everything in one code block as much as you can.
fireeyedboy
You need to use tabs or spaces to indent. You need newlines to changes lines. The key is: Don't use them outside of your php tags. Whitespace inside PHP tags is ignored. Whitespace outside of php tags is NOT ignored and is output to the browser. If there is whitespace outside of php tags before your header() call, it will cause an error because there was output before the header. In my example, all of the newlines and spaces are inside the php tags. In your code, they are all outside the php tags.
Scott Saunders
+1  A: 

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");
}
VolkerK
this time it doesn't show nat header problem and redirects to edit page..Thank You sir
diEcho
err... that shouldn't have **fixed** anything. It just added debug output. You shouldn't accept my answer at this state as a solution to your problem (it's really isn't). Are you absolutely sure you didn't change something else. Did you test the page the _exact same_ way as before?
VolkerK
i just rewrite code and rewrite all lines , but this time again i got header waring, it sucks me ....
diEcho
Which version (and build, i.e. where did you get that version of php) of php do you use? Ask because the "full" warning message including both locations (the one causing the output _and_ the one causing the warning) would be quite helpful to debug these problems.
VolkerK
i m using `PHP Version 5.2.9`
diEcho
That's the version.... Where did you get this version? E.g. "I use the version that ships with SomeLinux 45.2"
VolkerK