2

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • what is the problem you are experiencing? – Anthony Forloney Jan 30 '10 at 18:40
  • What's the 'header' problem? Also, I don't think it's a good thing to nest `ob_start` calls. – zneak Jan 30 '10 at 18:41
  • Please read this question on preventing SQL Injection. It's not related to your current problem, but it's very useful information. http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php – Kibbee Jan 30 '10 at 18:43
  • 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 linenumber2 – VolkerK Jan 30 '10 at 19:21
  • Can you add line 9 of add_book_hours.php to your question? – Karsten Jan 30 '10 at 19:33
  • line no. 9 is that where header() is called – xkeshav Jan 30 '10 at 20:16

5 Answers5

4

Is there any space before the first <?php?

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

Karsten
  • 14,572
  • 5
  • 30
  • 35
  • No space,and no UTF-8 BOM( exatcly i don't know waht is this) – xkeshav Jan 30 '10 at 19:02
  • 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 Jan 30 '10 at 19:14
  • (would +1 but out of votes) white space and BOM are always the first thing to check. – Pekka Jan 30 '10 at 19:42
  • 2
    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. – pestilence669 Jan 30 '10 at 19:58
4

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
  • 29,840
  • 14
  • 57
  • 64
  • 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? – xkeshav Jan 31 '10 at 08:31
  • I agree with Scott: you have a lot of unnecessary ` – Decent Dabbler Jan 31 '10 at 10:57
  • 2
    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 Jan 31 '10 at 15:48
3

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
  • 95,432
  • 20
  • 163
  • 226
  • this time it doesn't show nat header problem and redirects to edit page.. Thank You sir – xkeshav Jan 31 '10 at 10:57
  • 1
    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 Jan 31 '10 at 11:06
  • i just rewrite code and rewrite all lines , but this time again i got header waring, it sucks me .... – xkeshav Jan 31 '10 at 11:15
  • 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 Jan 31 '10 at 11:58
  • i m using `PHP Version 5.2.9` – xkeshav Jan 31 '10 at 15:28
  • That's the version.... Where did you get this version? E.g. "I use the version that ships with SomeLinux 45.2" – VolkerK Jan 31 '10 at 17:28
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');
?>

mariofertc
  • 383
  • 2
  • 7
0

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
  • 65,369
  • 27
  • 142
  • 182