By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

cannot modify header info, headers already sent

P: n/a
I have this section at the end of a page
-------------------
if ($_POST['assign']== 'Open in Excel'){

if (empty($data)) {
$data = "\n(0) Records Found!\n";}
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment; filename=".
$file_name.date("Y:m:d H:i").".xls");
header("Pragma: no-cache");
header("Expires; 0");

print "$header\n$data";

exit;

}else{
include "header.php";
include $template_path.$template;
include "footer.php";

}

Because the header.php is sent at the bottom of the page I can't have
the print to Excel output header in the if statement or I get the
message header already sent. I need the else statements where they
are because earlier in the code there are different selections of
templates depending on if a form is filled out so at the end it has to
display the right template.

Is there a way to send the excel output away from the page in a
function or something so that it processes the else?

thanks,
Oct 14 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
JRough wrote:
I have this section at the end of a page
-------------------
if ($_POST['assign']== 'Open in Excel'){

if (empty($data)) {
$data = "\n(0) Records Found!\n";}
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment; filename=".
$file_name.date("Y:m:d H:i").".xls");
header("Pragma: no-cache");
header("Expires; 0");

print "$header\n$data";

exit;

}else{
include "header.php";
include $template_path.$template;
include "footer.php";

}

Because the header.php is sent at the bottom of the page I can't have
the print to Excel output header in the if statement or I get the
message header already sent. I need the else statements where they
are because earlier in the code there are different selections of
templates depending on if a form is filled out so at the end it has to
display the right template.

Is there a way to send the excel output away from the page in a
function or something so that it processes the else?

thanks,
Nope. Once you've sent ANYTHING to the client, the web server sends
default headers.

You can send an Excel file or HTML in a request - but not both.

As has been explained to you time and time again...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 15 '08 #2

P: n/a
On Oct 14, 7:16*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
JRough wrote:
I have this section at the end of a page
-------------------
if ($_POST['assign']== 'Open in Excel'){
* * * * * *if (empty($data)) {
* * * * * * * * * *$data = "\n(0) Records Found!\n";}
* *header("Content-type: application/xmsdownload");
* *header("Content-Disposition: attachment; filename=".
$file_name.date("Y:m:d H:i").".xls");
* *header("Pragma: no-cache");
* *header("Expires; 0");
* *print "$header\n$data";
* *exit;
}else{
include "header.php";
* *include $template_path.$template;
* *include "footer.php";
}
Because the header.php is sent at the bottom of the page I can't have
the print to Excel output header in the if statement or I get the
message header already sent. * I need the else statements where they
are because earlier in the code there are different selections of
templates depending on if a form is filled out so at the end it has to
display *the right template.
Is there a way to send the excel output away from the page in a
function or something so that it processes the else?
thanks,

Nope. *Once you've sent ANYTHING to the client, the web server sends
default headers.

You can send an Excel file or HTML in a request - but not both.

As has been explained to you time and time again...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Okay, then maybe I can figure out how to redirect the excel if/excel
output to another page. the problem is then I have to requery the
database and run through all these queries depending on what a user
puts in a form. Is there a way to send the one query for the
selection to the redirected page so I don't have to go through all of
that?
thanks,
Oct 15 '08 #3

P: n/a
Message-ID:
<c8**********************************@s9g2000prg.g ooglegroups.comfrom
JRough contained the following:
>Okay, then maybe I can figure out how to redirect the excel if/excel
output to another page. the problem is then I have to requery the
database and run through all these queries depending on what a user
puts in a form. Is there a way to send the one query for the
selection to the redirected page so I don't have to go through all of
that?
thanks,
The best way is to re-arrange your logic so that all processing is done
before any page output. This may involve temporarily storing the output
as variables. Once the logic is finished you can either redirect using
header() or echo the output variables.

Like Jerry says, you can't do both.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
Oct 15 '08 #4

P: n/a
I don't know every detail of your current logic, but I would think you
could just have the input on this page and have it post that
information (like you already are doing here) to another page where
all the processing is done first. That way you have only one file
that accesses the database because it's doing all the work... instead
of splitting everything up amongst this one page.
Oct 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.