How to dump http request headers with PHP under CGI/FastCGI SAPI

Good old days of PHP installation exclusively as an Apache module are gone. Most of the modern PHP installations interact with a web server via FastCGI protocol. For instance nginx webserver and php-fpm daemon. This means that certain apache SAPI functions are not available anymore. One of this functions is apache_request_headers() to dump http headers sent by client.

From CGI specification rfc3875, section 4.1.18:

Meta-variables with names beginning with "HTTP_" contain values read
from the client request header fields, if the protocol used is HTTP.
The HTTP header field name is converted to upper case, has all
occurrences of "-" replaced with "_" and has "HTTP_" prepended to
give the meta-variable name.  

According to the specification above i wrote a piece of the PHP code that dumps all headers sent by client under any PHP SAPI:

foreach ($_SERVER as $key => $value) {
    if (strpos($key, 'HTTP_') === 0) {
        $chunks = explode('_', $key);
        $header = '';
        for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
            $header .= ucfirst(strtolower($chunks[$i])).'-';
        $header .= ucfirst(strtolower($chunks[$i])).': '.$value;
        echo $header.'<br>';


Post a Comment

Popular posts from this blog

Jqgrid show totals in the footer plugin

Zend Framework 2 AJAX: return JSON response from controller action. The proper way.

Memcached thread safe lock