{"id":292,"date":"2006-11-18T12:47:54","date_gmt":"2006-11-18T12:47:54","guid":{"rendered":"http:\/\/neosmart.net\/blog\/archives\/292"},"modified":"2023-01-28T15:53:51","modified_gmt":"2023-01-28T21:53:51","slug":"how-to-apache_response_headers-on-iis","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/how-to-apache_response_headers-on-iis\/","title":{"rendered":"How To: apache_response_headers() on IIS"},"content":{"rendered":"<p>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.\n<\/p>\n<p>IIS doesn\u2019t use this function, and before PHP5, there was no way to properly emulate its behavior. But on PHP5, it\u2019s nothing too difficult. Without further ado, here\u2019s the code:\n<\/p>\n<p><!--more--><pre lang='php'>&lt;?php \nif (!function_exists(&#039;apache_response_headers&#039;)){   \n\tfunction apache_response_headers(){   \n\t\t$nstList;   \n\t\tforeach (headers_list() as $nstHeader){   \n\t\t\t$nstObj      = substr($nstHeader, 0, strpos($nstHeader, &quot;: &quot;));   \n\t\t\t$nstObjVal   = substr($nstHeader,    strpos($nstHeader, &quot;: &quot;)+2);   \n\t\t\t$nstList&amp;#91;$nstObj&amp;#93; = $nstObjVal;   \n\t\t}   \n\t\treturn $nstList;   \n\t}   \n}\n?&gt;<\/pre><\/p>\n<p>It&#8217;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:\n<\/p>\n<pre lang='php'>&lt;?php \n\/\/generate some headers:\nif (!headers_sent()) {\n\theader(&#039;Content-Type: plain\/text&#039;); \n\theader(&quot;Cache-Control: no-cache, must-revalidate&quot;); \n\theader(&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;); \n\theader(&#039;Content-type: application\/pdf&#039;); \n\theader(&#039;Content-Disposition: attachment; filename=&quot;downloaded.pdf&quot;&#039;);\n}\n\n\/\/Test the output. See what&#039;s in the array. \nprint_r(apache_response_headers());\n?&gt;<\/pre>\n<p>It&#8217;s always a good idea to use <code>if(!headers_sent()):<\/code> before attempting to send your own headers \u2013 just in case your function is being called when it shouldn&#8217;t be. The code fragment above returns the following when executed:\n<\/p>\n<pre>Array\r\n(\r\n    [X-Powered-By] =&gt; PHP\/5.1.6\r\n    [Cache-Control] =&gt; no-cache, must-revalidate\r\n    [Expires] =&gt; Mon, 26 Jul 1997 05:00:00 GMT\r\n    [Content-type] =&gt; application\/pdf\r\n    [Content-Disposition] =&gt; attachment; filename=\"downloaded.pdf\"\r\n)\r\n<\/pre>\n<p>You can test it yourself on a normal Apache webserver, but you don&#8217;t have to \u2014 it&#8217;s exactly the same thing, line-for-line, byte-for-byte.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/neosmart.net\/blog\/how-to-apache_response_headers-on-iis\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[143,83,124],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-software","tag-apache","tag-iis","tag-php"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-4I","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":1,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":5016,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions\/5016"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}