Hi
I am getting this very error.....
Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\gallery2\examples\live-excel.php:36)

I have tried ob_start(); but of no use. It did not work....

can someone give the solution.??

Thanks in advance

cheers
Prati

use ob_start at the begining of the page

like <?php
ob_start();
?>

if its not working,please post your code

Thanks

Do as php_IND said, but use this code rather:

<?php ob_start("ob_gzhandler"); ?>

<?PHP
ob_start();
$host="localhost";
$user="root";
$pass="pulluru";
$con=mysql_connect($host,$user,$pass)or die("connection failed");
mysql_select_db("musicstore",$con);

$result = mysql_query("SELECT songTitle FROM musicstore");
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);


# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
//if ($data == "") {
// $data = "\nno matching records found\n";
//}

# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");
# replace $dbname.xls with whatever you want the filename to default to
# Default $dbname uses the Database name you are exporting from
header("Content-Disposition: attachment; filename=song.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "$header\n$data";

its working perfectly for me.I don't find any errors here.

Do as php_IND said, but use this code rather:

<?php ob_start("ob_gzhandler"); ?>

Hii
Thanks for the reply. but it did not work fully.....
the error is printed in the excel sheet.
what can be done?

include these codes

header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Pragma: public");

Hi
I am getting this very error.....
Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\gallery2\examples\live-excel.php:36)

I have tried ob_start(); but of no use. It did not work....

can someone give the solution.??

Thanks in advance

cheers
Prati

Output buffering is not supported on all servers. (Though most do).

To test if you have Output buffering support try this on a php page on your server::

<?php

ob_start();

echo 'dummy content';

if (headers_sent() || !ob_get_length()) {
   echo 'Your sever does not support output buffering';
} else {
   echo 'Output buffering supported';
  ob_clean();
}


?>

If your server does support output buffering, then it may be that the buffer is being dumped prematurely before you're sending your headers. This could be happening if you're using flush() or ob_flush() before you use header().

When you use the header() function, you should always check if headers have been sent or not...

Here's an example function to use in place of header():

function safe_header($str) {
   if (!headers_sent()) {
      header($str);
      return true;
   }
   return false;
}

What way you get a return of false if the header could not be sent.

Example Use:

if (safe_header('Location: login.php')) {
   die;
} else {
   echo '<script>location = 'login.php';</script>';
   echo 'Please <a href="login.php">Login</a>';
   die;
}

Note that if headers are already sent then you cannot send any more headers or you'll get the error you're getting. So instead you can send some content such as JS to try and redirect the page. If the user does not use JS, then you'll have to fallback to a link.

Hi didigtal-ether

Many thanks for a detailed reply.:)


I have tried the function you gave me and it worked.. (with 1 mysql error which i assume is my fault) The header error is solved.

I also executed the php script for testing server settings. It seems mine does not support and yet the header error is solved.

Thanks all

Cheers
Prati

Peace, Could I know what is the function ob__start(); ?
Thanks.
Best regards.

Hiii
ob_start -- Turn on output buffering
That means no output is sent from the script except from the headers. Instead it's stored in the internal buffer.

Peace, Could I know what is the function ob__start(); ?
Thanks.
Best regards.

Peace, Could I know what is the function ob__start(); ?
Thanks.
Best regards.

Heres the reference at php.net: http://www.php.net/ob_start

ob_start() is part of the output control functions in PHP. See: http://www.php.net/manual/en/ref.outcontrol.php

Calling ob_start() begins the buffer of output that is normally sent to HTTP (with PHP on a webserver).

Do to the HTTP specification, you cannot send any HTTP Headers after you have sent some HTTP Content. ob_start() allows you to have the HTTP Content placed in a buffer, so that you can send HTTP headers at any time. Then at the end of your PHP script, call ob_end_flush() which will flush the HTTP Content.

Edit: Looks like php_coder got to your question first. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.