943,866 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 12646
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 21st, 2007
0

Cannot modify header information.Headers already sent

Expand 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
Last edited by php_coder; May 21st, 2007 at 4:03 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
php_coder is offline Offline
34 posts
since Dec 2006
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

use ob_start at the begining of the page

like <?php
ob_start();
?>

if its not working,please post your code

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
php_IND is offline Offline
4 posts
since May 2007
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

Do as php_IND said, but use this code rather:

PHP Syntax (Toggle Plain Text)
  1. <?php ob_start("ob_gzhandler"); ?>
Reputation Points: 9
Solved Threads: 1
Newbie Poster
smalldog is offline Offline
20 posts
since May 2007
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

<?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";
Reputation Points: 10
Solved Threads: 0
Light Poster
php_coder is offline Offline
34 posts
since Dec 2006
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

its working perfectly for me.I don't find any errors here.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
php_IND is offline Offline
4 posts
since May 2007
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

Click to Expand / Collapse  Quote originally posted by smalldog ...
Do as php_IND said, but use this code rather:

PHP Syntax (Toggle Plain Text)
  1. <?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?
Reputation Points: 10
Solved Threads: 0
Light Poster
php_coder is offline Offline
34 posts
since Dec 2006
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

include these codes

PHP Syntax (Toggle Plain Text)
  1. header("Content-type: application/vnd.ms-excel");
  2. header("Content-Disposition: attachment; filename=\"$filename\"");
  3. header("Expires: 0");
  4. header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
  5. header("Pragma: public");
Reputation Points: 10
Solved Threads: 0
Newbie Poster
php_IND is offline Offline
4 posts
since May 2007
May 21st, 2007
0

Re: Cannot modify header information.Headers already sent

Click to Expand / Collapse  Quote originally posted by php_coder ...
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 Syntax (Toggle Plain Text)
  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():

php Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
May 22nd, 2007
0

Re: Cannot modify header information.Headers already sent

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
Reputation Points: 10
Solved Threads: 0
Light Poster
php_coder is offline Offline
34 posts
since Dec 2006
Jun 3rd, 2007
0

Re: Cannot modify header information.Headers already sent

Peace, Could I know what is the function ob__start(); ?
Thanks.
Best regards.
Reputation Points: 44
Solved Threads: 23
Posting Whiz
Pro2000 is offline Offline
350 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: To Professionals only
Next Thread in PHP Forum Timeline: BCC: Multiple Recipient





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC