Search This Blog

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:

It is a quite common problem when cache is expired and multiple PHP processes are trying to update it. There is even a special term for this - "cache slam".
The "cache slam" problem is described very good on php.net

"If a highly shared cache entry stored, for example, in Memcache expires, many clients gets a cache miss. Many client requests can no longer be served from the cache but try to run the underlying query on the database server. Until the cache entry is refreshed, more and more clients contact the database server. In the worst case, a total lost of service is the result."

You can't do $memcached->set('lock_xyz') and then in some other place if ($memcached->get('lock_xyz')) like described in a lot of suggestions all over the internet. Imaging that 10 processes in parallel are trying to check whether lock exists and if not - set it. All of them will end up with setting the lock and do the cache refresh process. In other wo…