HTML Compression via PHP or Apache

Introduction

PHP, as a server based scripting language, is more than common nowadays. Most internet providers supply PHP in a form, which is compiled by default or extended with various addons by "./configure".
The packing functionality, which we want to talk about, is always available using PHP's embedded gzip handler. PHP's packing handlers can be extended with zlib using the '--with-zlib' or bzip ('--with-bzip2') compilation switches. This is non relevant for this article but MUST be kept in mind, using the informations found in this article
(-> PHP ini.zlib.output-compression).
This article is intended to be used without default compression enabled by Apache and/or static PHP output compression like zlib/bzip.

Points of interrest

The PHP embedded gzip handler.
In PHP version 4.0.4 (currently 4.3.4 [November 2003]) the PHP coders introduced ob_start("ob_gzhandler") which extremely simplified the browser relevant code to deliver packed data from PHP generated content. ob_start("ob_gzhandler") enables PHP to transparently deliver "gzip", "deflate" or "none" encoded data, without any code's knowledge about the client browsers cpabilities or operating system.
  -> see ob-gzhandler

Real World

Mostly outhere, we have two types of Homepages where the content of this articls can hook in and boost the current pages speed, because of less data to be transferred.
The first type are usual PHP driven pages, which we will extend to beeing packed and cached via PHP. The second type are standard (X/D)HTML sites will we will redirect to PHP via Apaches "mod_rewrite" extension and deliver them packed and cached also.
Additionally, at the HTML part, we will introduce some regular epressions, which supports us in the attempt of "inlining" filebased Javascript and CSS includes, to get delivered packed too.
Finally we will show a PHP based cache mechanism, which is capable of storing informations about the files which are used to generate the pages content. This enables the cache to know about all sources included in the generation of the pages data and let it invalidate when one of the relevant files (HTML, PHP, included CSS or JS) changes.

Let's start

Lets start with some basics.

Packing with PHP ob_start

As already mentioned, ob_start("ob_gzhandler") does all the magic of gzip-encoding without fancy setup or code.
index.php:
<?php
	include( 'something.inc' );
	include( 'something_else.inc' );
 
	ob_start( 'ob_gzhandler' );
 
	/*
		your normal code begins here...
		even the output buffer mechanisms
		still work with gzip buffering enabled. 
 
			:
			:
	*/			
	echo "Welcome to the packed world."; 
?>
 
As you see, simply adding the ob_gzhandler to your code will deliver packed pages, when capable, to the clients browser. When not, PHP tries "deflate" encoding or, at least, "none", which delivers the page as it is.

Packing with apaches mod_rewrite

http://web4.to-ol.net/irgend/eine/seite/an/einem/total/verrueckten_ort.h...
#Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)(.html$) redir.php?id=$1$2 [L]
 
<?php
    echo "site: {$_GET['id']}<br />";
    echo "param: ".substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'],"?") );
    echo "<br />\n<img src='null.gif' width='1' height='800' />\n";
    echo "look text above me";
    echo "<a name='12'></a>";
?>
 

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • No HTML tags allowed.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.
© 2010 khype.org.. Drupal theme by Kiwi Themes.