Content-encoding: gzip

Note: as a result of the screwing around with PHP and ob_gzhandler I did, I got into content negotiation which seemed to be a better way to serve non-PHP content, rather than wrapper that content in PHP. I wrote up an article on this here: Content Negotiation (aka Multi-Format Processing) for CSS and RSS.

I've been trying out using various compression techniques with content on my various sites. One option available with PHP is to turn on output buffering (ob_start) and use the ob_gzhandler callback function to handle the compression.

This works, sort of.

What I'm finding is that either output buffering, or the ob_gzhandler routine, or both are not functioning as expected.

I'm finding that the handler returns at minimum the length of the compressed output (pre-pended to the output), followed by the output, followed by "\n0\n". Now, this may be exactly what is expected for transfer-encoding gzip, I'm not sure (i'm digging around for this tomorrow).

The other problem I noticed is that I was emitting a Content-Length header for my pages (to be a good net.citizen and all that). However, the ob_gzhandler routine does not add or change a Content-Length header. This would not be a problem if the length returned from ob_get_lenth was accurate, but that length reflects the uncompressed data stream. There appears to be no supported method of getting the length of the compressed datastream.

Except, as I've learned, something is dumping that length back to the HTTP client, in hex.

Following is a snippet of PHP for demonstration purposes:


<?php
ob_start('ob_gzhandler');
print "<html>\n<head>\n<title>This is a title</title>\n</head><body>
<h1>Heading</h1>\n
<p>blah</p></body></html>";
while(@ob_end_flush());
?>

«Another Phishy Note | Main |How to get the last day's content or last post »

:
:

Enter your email address:

Delivered by FeedBurner