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:
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:
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:
Array ( [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.