{"id":371,"date":"2015-07-14T15:23:21","date_gmt":"2015-07-14T20:23:21","guid":{"rendered":"http:\/\/www.popmartian.com\/tipsntricks\/?p=371"},"modified":"2015-07-14T15:26:18","modified_gmt":"2015-07-14T20:26:18","slug":"howto-use-php-getallheaders-under-fastcgi-php-fpm-nginx-etc","status":"publish","type":"post","link":"https:\/\/www.popmartian.com\/tipsntricks\/2015\/07\/14\/howto-use-php-getallheaders-under-fastcgi-php-fpm-nginx-etc\/","title":{"rendered":"HOWTO use PHP getallheaders() under FastCGI (PHP-FPM, nginx, etc.)"},"content":{"rendered":"<p>If you use Nginx, PHP-FPM or any other FastCGI method of running PHP you&#8217;ve probably noticed that the function getallheaders() does not exist. There are many creative workarounds in the wild, but PHP offers two very nice features to ease your pain.<\/p>\n<p>First, PHP has an internal function called <a href=\"http:\/\/php.net\/manual\/en\/function.function-exists.php\">function_exists()<\/a> which lets you see if a function is already declared.\u00c2\u00a0 With namespaces and objects this isn&#8217;t a very big deal so many people don&#8217;t know about this little function, but when a built-in function comes and goes, it can be a lifesaver.<\/p>\n<p>Second, PHP lets you declare a function that has global scope if you are inside a function that has global scope.<\/p>\n<p>If we put them together, we have a few lines we can include before we process any headers that will declare <a href=\"http:\/\/php.net\/manual\/en\/function.getallheaders.php\">getallheaders()<\/a> if it does not exist.  If it does exist, nothing changes.<\/p>\n<p><code>if (!function_exists('getallheaders')) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;function getallheaders() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$headers = [];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;foreach ($_SERVER as $name =&gt; $value) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (substr($name, 0, 5) == 'HTTP_') {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return $headers;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n}<\/code><\/p>\n<p>This idea was shamelessly lifted from the user contributed comments at <a href=\"http:\/\/php.net\/manual\/en\/function.getallheaders.php\">http:\/\/php.net\/manual\/en\/function.getallheaders.php<\/a>.  Thanks joyview at gmail dot com<\/p>\n<blockquote><p>Did you find this post useful or have questions or comments? Please let me know!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>If you use Nginx, PHP-FPM or any other FastCGI method of running PHP you&#8217;ve probably noticed that the function getallheaders() does not exist. <a href=\"https:\/\/www.popmartian.com\/tipsntricks\/2015\/07\/14\/howto-use-php-getallheaders-under-fastcgi-php-fpm-nginx-etc\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11,9],"tags":[],"class_list":["post-371","post","type-post","status-publish","format-standard","hentry","category-how-to-do-stuff","category-php","category-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":14,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":386,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/371\/revisions\/386"}],"wp:attachment":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}