Posts

Showing posts from October, 2012

YUI javascript compressor and PHP tags

YUI compressor is very useful and powerful tool for javascript files compression. Many developers compress their static javascript files but what about files with PHP tags? With something like this: var username = "<?php echo $username; ?>"; or like this function() { var some_code_before; <?php if (Acl::check("finance")) { ?> Ext.addShortcut('finance'); <?php } ?> var some_code_after; } First example will work, but YUI will throw an exception on second. And also this piece of javascript code does not pass validation. Lets wrap PHP tags into comments: function() { var some_code_before; /*<?php if (Acl::check("finance")) { ?>*/ Ext.addShortcut('finance'); /*<?php } ?>*/ var some_code_after; } Now this code is valid. But lets try to feed it to YUI: # yui test.js function(){var b;Ext.addShortcut("finance");var a}; PHP tags are stripped away by YUI and ...

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 = e...