dan forys

blog

Unsetting HTTP headers in PHP

Posted on 2008-08-06 - Comments

I just came across a subtle issue affecting Internet Explorer users (well, fancy that!) and HTTPS connections.

One of my clients has a site that downloads a series of results as a CSV file, which they open in Excel. Unfortunately, Internet Explorer was refusing to download the file, and was presenting an error message reading "Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found."

To add to my confusion, this was happening on the live server (PHP4), but not on my dev server (PHP5) which both use the same code.

In the end, I happened upon a Microsoft Knowledge Base article that explained the problem. Basically, IE obeys any "no-cache" headers you send to the browser. Without caching the file, Office applications cannot open the file when served over HTTPS.

How to solve the issue? Remove the cache header(s) – but how?

The PHP manual doesn't make it entirely clear, and I found the solution by accident. To remove a header, use the same syntax as for setting a header, but only include a space after the colon.

For example, for the "Pragma" header:

header('pragma: ');

Note: You must include the space after the ":" or the header will not be unset.