How To: apache_response_headers() on IIS

Along with the release of our request_uri for IIS yesterday, we have another useful tip for a second function commonly found in WordPress plugins and other PHP redistributable scripts. apache_response_headers is used to get a list of all the headers sent out by a page, in the format of an associative array.

IIS doesn’t use this function, and before PHP5, there was no way to properly emulate its behavior. But on PHP5, it’s nothing too difficult. Without further ado, here’s the code:

[sourcecode language=’php’][/sourcecode]

It’s fairly straight forward code that involves changing the contents of a normal array into an associative one with a part of the string as the index name. It works because we know what form the content of the original array is going to take. To test the code:

[sourcecode language=’php’][/sourcecode]

It’s always a good idea to use if(!headers_sent()): before attempting to send your own headers – just in case your function is being called when it shouldn’t be. The code fragment above returns the following when executed:

    [X-Powered-By] => PHP/5.1.6
    [Cache-Control] => no-cache, must-revalidate
    [Expires] => Mon, 26 Jul 1997 05:00:00 GMT
    [Content-type] => application/pdf
    [Content-Disposition] => attachment; filename="downloaded.pdf"

You can test it yourself on a normal Apache webserver, but you don’t have to — it’s exactly the same thing, line-for-line, byte-for-byte.

5 thoughts on “How To: apache_response_headers() on IIS

  1. Just a tidbit about headers_sent()… in properly written code, you should be checking the other way around and raising some sort of exception if headers have been sent and if they have not, continue and send them. Otherwise errors in your code will fail silently and probably have unintended consequences:

    The biggest failure I saw in response to bad header logic was a set of dynamic CSS documents that were supposed to be cached, but by the time the cache statements were sent, headers were already sent, thus denying browsers from caching the file, leading to much higher bandwidth and server usage. Which is a bad thing(tm).

  2. Good point Ceph!

    I just tested the output of this code and using the curl library fed the contents of the apache_response_headers from a real Apache server and then strcmpd’d the two values — a perfect match.

    Thank you for this excellent tidbit of code NeoSmart!!

    I’m looking at the Request_URI for Windows thing, it seems like what I need to get Media-Wiki to correctly parse my rewritten URIs.

    I absoloutely love IIS and the power it gives the end user as well as the stability (much better than Apache!), but the fact remains that the majority of the scripts out there today are/were written with Apache in mind. I think between this apache_response_headers for IIS patch and your request_URI patch I can finally get things working!! :D

  3. Just a quick tip:

    I just came across this site from the PHP documentation.
    Here’s a quick suggestion for anyone using both NeoSmart’s request_uri for IIS *and* this cool apache_response_headers():

    Copy the function above to that you extracted to the php folder in the setting up of request_uri for Windows. That way it too will always be present without needing to modify future scripts.

  4. Just looking around and saw Cyph‘s comment on dynamic CSS, got intrigued, Googled it, and found out it was yet another NeoSmart article (we should abbreviate that: YANA, you guys have too many useful articles!): 276

    You guys are the greatest!

Leave a Reply

Your email address will not be published. Required fields are marked *