Link to home
Start Free TrialLog in
Avatar of mamuscia
mamusciaFlag for United States of America

asked on

PHP Headers for Exporting Excel in SSL

I am having a very difficult time getting an export of an Excel file to work in a HTTPS/SSL environment.  When I run the script in regular HTTP non-SSL environment it works fine.
What are the proper HEADERS I need to use when downloading an Excel file in a SSL website?

I've tried all kinds of headers based on similar issues I found on web searches, but nothing works.

I am using IE 7 with PHP5.2 Apache
<?php
  session_start();
  $sessvar = $_GET['sessvar'];
  $htmltab = $_SESSION[$sessvar];
  $filename = "export.xls";
  header('Content-Type: application/vnd.ms-excel');
  header('Pragma: private');
  header('Cache-control: private, must-revalidate');
  header("Content-Disposition: attachment; filename=$filename");
  header("Expires: 0");
  header("Keep-Alive: timeout=15, max=100");
  header("Connection: Keep-Alive");
  header("Transfer-Encoding: chunked");
  print $htmltab;
 ?>

Open in new window

Avatar of Steve Bink
Steve Bink
Flag of United States of America image

AFAIK, there should be no difference in the HTTP headers.  SSL should be transparent at that level.  Use the same headers you are using for your normal download.  

When you say "nothing works", what actually happens?
Avatar of mamuscia

ASKER

I know for sure it is SSL getting in the way as it works under HTTP, but not HTTPS.

Here is the process of events and the results:

1 - display a form to user from PHP program
2 - user fills in date ranges on form and submits
3 - the same PHP script is invoked and the $_POST variables picked up
4 - the PHP script then does a MySQL query using the parameters the user provided in the form
5 - the script then creates an HTML table from the results of the MySQL query
6 - the HTML table contents is put into a session variable so the export script can pick it up
6 - there is an ONLOAD event triggered that starts a javascript to open a new window
7 - the new window opens, picks up the HTML table contents from the session variable, then
tries to force the download as an EXCEL file using the script example I provided above.

What happens at this point is IE has opened the new window, but it just flashes and ends without downloading the EXCEL file.  Again, when done in HTTP, it opens the download dialog box asking me to OPEN/SAVE/CANCEL.  I  am able to get the EXCEL file in HTTP.  But in HTTPS, the new window opens, then closes without putting up the download dialog box.

I have read many forums and other sites that had similar issues with HTTPS.  They suggest many different HEADER solutions, I've tried them all, but none have worked.

By the way, I can STOP the browser when the new window opens and then refresh that window.
The first time I refresh it, it gives me a windows IE error message saying the web site cannot be found.  But if I stop it and refresh it again, the download dialog works.    See the attached file of the picture of the IE error window I get when I stop it the first time.

I think this is an IE issue with HTTPS as it works fine in FIREFOX.

I came to EXPERTS-EXCHANGE because I usually get solutions that work.

Hope you, or someone else knows how to fix this...thanks
IE-Error.jpg
I did see this and I stripped the code so that no cache references were made at all.  

header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$filename");

I installed IE 8 and it still happens and the article you point to above does not have a fix for IE 8, it only goes up to IE 6.

There has got to be a solution to this as I'm sure other sites have run into similar issues.
What about saving the table to a temp file, then forwarding the user to it?
I tried putting the generated table into a file on the server, then downloading it for the user.  I get the same error where IE says it cannot load or find the website....

Do you know anything about the PEAR install for Spreadsheet_Excel_Writer?  Will it work in an HTTPS environment?  I am going to try to install that and use it for manipulating Excel files.  
I've used that package, and it works pretty well.  A few formatting issues, but a good API for creating quick and dirty spreadsheets.

The problem here is likely not in your code.  SSL works at a different level.  The server processes the PHP file, runs the script, and generates an output.  That output is what is transmitted to the client.  Using SSL encrypts that transmission instead of allowing it go on the wire as plain-text.

I'll see if I can find some time over the next couple days to experiment with some file download code and SSL.  I created a downloadable EDI export file for another SSL-only website I managed...I'll look over that code for anything particular to make it work, but I have never encountered the issue you are reporting.
Thanks for helping research/test this.  I installed the spreadsheet excel writer and it generates the Excel file nicely.  It works in Firefox as always, but in IE it still does not work.  I will continue to experiment and search as well.  Maybe we can come up with a solution here.
>>> It works in Firefox as always, but in IE it still does not work.

Specifically, what doesn't work in IE but works in FF?
In firefox, it opens a prompt box to OPEN/SAVE/CANCEL the generated EXCEL file.  In IE, it gives me the message that says it cannot open the website.  See my very first entry above.  I've continued researching and it must have something to do with cache/no-cache and https, but for the life of me, I cannot come up with the right combination of HEADER statements in my PHP script.  I also have seen articles explaining SESSION caching options, but again, when I try those solutions none work.
ASKER CERTIFIED SOLUTION
Avatar of mamuscia
mamuscia
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial