User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,913 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,708 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4237 | Replies: 11
Reply
Join Date: Dec 2006
Posts: 34
Reputation: php_coder is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
php_coder php_coder is offline Offline
Light Poster

Question Cannot modify header information.Headers already sent

  #1  
May 21st, 2007
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
Last edited by php_coder : May 21st, 2007 at 3:03 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Posts: 4
Reputation: php_IND is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
php_IND php_IND is offline Offline
Newbie Poster

Re: Cannot modify header information.Headers already sent

  #2  
May 21st, 2007
use ob_start at the begining of the page

like <?php
ob_start();
?>

if its not working,please post your code

Thanks
Reply With Quote  
Join Date: May 2007
Posts: 20
Reputation: smalldog is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
smalldog smalldog is offline Offline
Newbie Poster

Re: Cannot modify header information.Headers already sent

  #3  
May 21st, 2007
Do as php_IND said, but use this code rather:

<?php ob_start("ob_gzhandler"); ?>
Join a forum for webmasters and developers
HTML Forum
Reply With Quote  
Join Date: Dec 2006
Posts: 34
Reputation: php_coder is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
php_coder php_coder is offline Offline
Light Poster

Re: Cannot modify header information.Headers already sent

  #4  
May 21st, 2007
<?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";
Reply With Quote  
Join Date: May 2007
Posts: 4
Reputation: php_IND is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
php_IND php_IND is offline Offline
Newbie Poster

Re: Cannot modify header information.Headers already sent

  #5  
May 21st, 2007
its working perfectly for me.I don't find any errors here.
Reply With Quote  
Join Date: Dec 2006
Posts: 34
Reputation: php_coder is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
php_coder php_coder is offline Offline
Light Poster

Re: Cannot modify header information.Headers already sent

  #6  
May 21st, 2007
Originally Posted by smalldog View Post
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?
Reply With Quote  
Join Date: May 2007
Posts: 4
Reputation: php_IND is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
php_IND php_IND is offline Offline
Newbie Poster

Re: Cannot modify header information.Headers already sent

  #7  
May 21st, 2007
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");
Reply With Quote  
Join Date: Sep 2005
Posts: 641
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Cannot modify header information.Headers already sent

  #8  
May 21st, 2007
Originally Posted by php_coder View Post
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::

  1. <?php
  2.  
  3. ob_start();
  4.  
  5. echo 'dummy content';
  6.  
  7. if (headers_sent() || !ob_get_length()) {
  8. echo 'Your sever does not support output buffering';
  9. } else {
  10. echo 'Output buffering supported';
  11. ob_clean();
  12. }
  13.  
  14.  
  15. ?>

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():

  1. function safe_header($str) {
  2. if (!headers_sent()) {
  3. header($str);
  4. return true;
  5. }
  6. return false;
  7. }

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

Example Use:

  1. if (safe_header('Location: login.php')) {
  2. die;
  3. } else {
  4. echo '<script>location = 'login.php';</script>';
  5. echo 'Please <a href="login.php">Login</a>';
  6. die;
  7. }

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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Dec 2006
Posts: 34
Reputation: php_coder is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
php_coder php_coder is offline Offline
Light Poster

Re: Cannot modify header information.Headers already sent

  #9  
May 22nd, 2007
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
Reply With Quote  
Join Date: Jun 2007
Posts: 71
Reputation: Pro2000 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pro2000's Avatar
Pro2000 Pro2000 is offline Offline
Junior Poster in Training

Re: Cannot modify header information.Headers already sent

  #10  
Jun 3rd, 2007
Peace, Could I know what is the function ob__start(); ?
Thanks.
Best regards.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 7:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC