Possible Duplicate:
Header Location + Content Disposition
I have a page that generates excel report using header content-type and save the generation information on the database (e.g. who generated the report,when etc.) My problem is why I can’t redirect the page. below is the sample code/algo
// excel content
/* excel content populates here */
// output to excel file
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$positionFileName);
header("Cache-Control: max-age=0");
// save logs on the database
/* save logs code executed here */
// redirect page to get the logs on the database that display on the web page
header("location: report.php"); *<--- I can't redirect to report.php*
Here is exactly what I want
the user will do is to select type of report then click button generate to output the excel report (open/save dialog). then after that, the system will going to save the logs of generated report on the database then show it on the page. that’s why I want to redirect it again on report.php so that the logs will be get again on the database to display on browser.
The headers are colliding. First you say “here comes a document”, using
Content-Disposition: attachment, and then you say “Oh no, it’s not coming, do an additional request to XYZ”, using theLocationheader. You can omit theContent-headers to achieve the same result.This however works for me:
In that I get redirected to the foo.dat file, which is displayed in the browser (so the
Content-headers are ignored, since after theLocation-header a new request is issued which does not get theContent-headers in its response. If you want to enforceattachment, either configure your web server to emit those headers for certain extensions or directories, or usefpassthru($filename)without theLocation-header to directly emit the file without running into memory errors:But please note that any output generated during the executing of the code after the first three
header()statements will be outputted to the browser, which’ll think it is file data.I guess this is currently also happening, causing the
header("location: ...")to fail. Enable error reporting to see why and where there was data printed.