JaguarPC managed web hosting logo
JaguarPC HomeWeb Hosting ForumHosting client login
Hosting Sales - 1.800.558.5869
Order Web Hosting Now
Welcome Panel

You are not logged in. Please login below.

Statistics

Threads: 20,250, Posts: 149,925, Members: 41,383

Welcome to our newest member, finanxo

Specials

View our Web Hosting Specials



Get your own merchant account!



JaguarPC Community - Web Hosting, VPS Hosting, cPanel VPS Hosting, Hybrid Servers, Dedicated Servers, Virutal Private Servers, Managed Servers » Hosting Community Support » Design and Development : Warning: Cannot modify header information - headers already sent by (output started a

Design and Development Get help setting up forums, databases, or discuss any form of scripting. Have a question or comment about a website? Or just show off your own site!

Reply
 
Thread Tools Display Modes
Old 10-19-2006, 09:32 PM   #1
chinna
JPC Member
 
Join Date: Oct 2006
Posts: 19
Warning: Cannot modify header information - headers already sent by (output started a

hi

iam new php, now i want write a html form datas to Excel file, for that i wrote following code, but it displaying errors like "Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\Apache\htdocs\sample\excel.php:9) in c:\program files\apache group\Apache\htdocs\sample\excel.php on line 56".


Code :


<?php

$db=mysql_connect('localhost', 'root', 'success');
if(!$db)
{
echo "error in connecting.............";
}
mysql_select_db('byt');
$result = mysql_query('select * from users');
$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{
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);


if ($data == "") {
$data = "\nno matching records found\n";
}

header('Content-type: application/octet-stream');

header('Content-Disposition: attachment; filename=excelfile.xls');
header('Pragma: no-cache');
header("Expires: 0");

echo $header."\n".$data;
?>


whats the wrong with code.....

pls help me its urgent task for me

Regards
Chinna
chinna is offline   Reply With Quote
Old 10-19-2006, 10:10 PM   #2
the_ancient
Community Liason
 
Join Date: Feb 2004
Posts: 2,972
Blog Entries: 1
the only thing I see right off is

Code:
if(!$db)
{
echo "error in connecting.............";'
}
If you get a Db error here it will throw the echo statment and then try to pass header info which will not be allowed as the headers are sent BEFORE the text

Change it to

Code:
if(!$db)
{
echo "error in connecting.............";'
die();
}
Or you can use
Code:
if(!$db)
{
echo "error in connecting.............";'

}else{
making sure to add another "}" at the end of the code
__________________
-------------------------
the_ancient
Cyclone Software Solutions
Web Design - Graphic Design & Printing - Virtual PBX Systems
the_ancient is offline   Reply With Quote
Old 10-19-2006, 10:21 PM   #3
mattsiegman
Jag Veteran
 
mattsiegman's Avatar
 
Join Date: Sep 2001
Location: Wichita, KS
Posts: 1,641
also, it will break if you have any space before your <?
__________________
I do not work for Jaguar PC. My responses are not official policy for JPC. I simply moderate the forums.
mattsiegman is offline   Reply With Quote
Old 10-19-2006, 10:21 PM   #4
Vin DSL
Yeah, I know a LOT!
 
Vin DSL's Avatar
 
Join Date: Mar 2003
Location: Arizona Uplands Intelligence Quotient: 138+
Posts: 10,384
Pretty self-explanatory! You're calling the header twice and that's a no-no...

Comment out one of those lines, and it should take care of itself.
__________________
DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

No Guts, No Story! VinDSL © 2010
Vin DSL is offline   Reply With Quote
Old 10-19-2006, 10:24 PM   #5
Ron
Now with 46.3% more slack
 
Join Date: Aug 2002
Posts: 5,678
Blog Entries: 1
Use the php tags around php code in the forum please...
PHP Code:
<?php

$db
=mysql_connect('localhost''root''success');
if(!
$db)
{
    echo 
"error in connecting.............";
            die(
mysql_error());
}
if (! 
mysql_select_db('byt'))
{
            die(
mysql_error());
}
if (! 
$result mysql_query('select * from users'))
{
            die(
mysql_error());
}

$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{
      
$value str_replace('"''""'$value);
      
$value '"' $value '"' "\t";
    }
    
$line .= $value;
  }
  
$data .= trim($line)."\n";
}
  
$data str_replace("\r"""$data);


if (
$data == "") {
  
$data "\nno matching records found\n";
}

header('Content-type: application/octet-stream');

header('Content-Disposition: attachment; filename=excelfile.xls');
header('Pragma: no-cache');
header("Expires: 0");

echo 
$header."\n".$data;
?>
I think your error is in your mysql_query() call... because the error message says that headers were output on line 9 of your code.

What is happening is that the error or warning itself is sending output and headers. You can shut off warnings and error output, but you'll have to fix the errors anyway.

Try adding the lines as I suggest in your code above.

Good luck.

Last edited by Ron; 10-19-2006 at 10:27 PM. Reason: typo
Ron is offline   Reply With Quote
Old 10-19-2006, 10:29 PM   #6
the_ancient
Community Liason
 
Join Date: Feb 2004
Posts: 2,972
Blog Entries: 1
Quote:
Originally Posted by Ron View Post
Use the php tags around php code in the forum please...
.
yes dad
__________________
-------------------------
the_ancient
Cyclone Software Solutions
Web Design - Graphic Design & Printing - Virtual PBX Systems
the_ancient is offline   Reply With Quote
Old 10-19-2006, 10:35 PM   #7
Ron
Now with 46.3% more slack
 
Join Date: Aug 2002
Posts: 5,678
Blog Entries: 1
[code] Tags are ok, too...
Ron is offline   Reply With Quote
Old 10-19-2006, 10:36 PM   #8
chinna
JPC Member
 
Join Date: Oct 2006
Posts: 19
thanks for replyed, i tryed the above suggestions but still iam getting same errors...

Regards
Chinna
chinna is offline   Reply With Quote
Old 10-19-2006, 10:39 PM   #9
Ron
Now with 46.3% more slack
 
Join Date: Aug 2002
Posts: 5,678
Blog Entries: 1
No way, unless this code is being called from another program. The line number in the error would change at a minimum.
Ron is offline   Reply With Quote
Old 10-19-2006, 10:44 PM   #10
chinna
JPC Member
 
Join Date: Oct 2006
Posts: 19
the following code also displaying Warning: Cannot modify header information - headers already sent by

Quote:
<?php
header('Location: http://www.example.com/');
?>

is there any specific configuration in php.ini to use "header"?..

Regards
Chinna

Last edited by chinna; 10-19-2006 at 10:55 PM.
chinna is offline   Reply With Quote
Old 10-19-2006, 10:49 PM   #11
Vin DSL
Yeah, I know a LOT!
 
Vin DSL's Avatar
 
Join Date: Mar 2003
Location: Arizona Uplands Intelligence Quotient: 138+
Posts: 10,384
Quote:
Originally Posted by chinna View Post
is there any specific configuration in php.ini to use "header"?...
Hrm...

That's a thought!

You might try adding or changing the state of:

Code:
output_buffering=on;
__________________
DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

No Guts, No Story! VinDSL © 2010
Vin DSL is offline   Reply With Quote
Old 10-19-2006, 10:55 PM   #12
the_ancient
Community Liason
 
Join Date: Feb 2004
Posts: 2,972
Blog Entries: 1
Quote:
Originally Posted by chinna View Post
the following code also displaying Warning: Cannot modify header information - headers already sent by
PHP Code:
<?php
header
('Location: http://www.example.com/');
?>
is there any specific configuration in php.ini to use "header"?..

Regards
Chinna
In addition to vins suggestion make sure:
Quote:
Originally Posted by mattsiegman View Post
also, it will break if you have any space before your <?
you can not send ANYTHING at all before you send header, no html, no text, NOTHING.

Make sure your not
__________________
-------------------------
the_ancient
Cyclone Software Solutions
Web Design - Graphic Design & Printing - Virtual PBX Systems
the_ancient is offline   Reply With Quote
Old 10-19-2006, 10:58 PM   #13
Ron
Now with 46.3% more slack
 
Join Date: Aug 2002
Posts: 5,678
Blog Entries: 1
hmmmmmm

I don't think a space before <? will cause this problem inside a .php file; perhaps in a .html file.... OR if he's got a handler set to handle .php as parsed .html?

How is this code being called? Straight from a URL or from another script?
Ron is offline   Reply With Quote
Old 10-19-2006, 11:10 PM   #14
chinna
JPC Member
 
Join Date: Oct 2006
Posts: 19
thanks thanks a lot i found a error... its the space before <?php ..now working properly....thanks ron .. i will meet with next problem..

best reagrds
Chinna
chinna is offline   Reply With Quote
Old 10-19-2006, 11:12 PM   #15
the_ancient
Community Liason
 
Join Date: Feb 2004
Posts: 2,972
Blog Entries: 1
Quote:
Originally Posted by Ron View Post
hmmmmmm

I don't think a space before <? will cause this problem inside a .php file; perhaps in a .html file.... OR if he's got a handler set to handle .php as parsed .html?

How is this code being called? Straight from a URL or from another script?
yes it will, anything outside, even a single space, of the <? ?> tages is sent to the browser which means the headers where also sent,

Test it...
__________________
-------------------------
the_ancient
Cyclone Software Solutions
Web Design - Graphic Design & Printing - Virtual PBX Systems
the_ancient is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump