|
To lighten the time of load of our pages generated with PHP, we can send them to the navigator compressed with GZip To lighten the time of load of our pages generated with PHP, we can send them to the navigator compressed with GZip using the functions of control of exit, for it, we will call to the predefined function ob_gzhandler how dealer of the function ob_start, we see an example:
<?
ob_start ("ob_gzhandler");
//Content of the page, it can contain
//so much HTML how PHP
ob_end_flush ();
?>
To bear in mind that the whole content must be in the place indicated therefore the first characters of the document must be <? and two last ones?> and it is not necessary to add anything except where it is indicated, if we do not go carefully we will receive an error similar to the following one:
Warning: Cannot add header information...
Another more finished form still of compression, it consists of applying the same function, but eliminating in turn the spaces and jumps of line of the source of the document, what will not have any visual effect but it will diminish the discharge time, let's see how to do it:
<?
ob_start ();
//Content of the page, it can contain
//so much HTML how PHP
$cntACmp =ob_get_contents ();
ob_end_clean ();
$cntACmp=str_replace ("\n", '', $cntACmp);
$cntACmp=ereg_replace ('[[:space:]] +', '', $cntACmp);
ob_start ("ob_gzhandler");
I begin $cntACmp;
ob_end_flush ();
?>
This method works just as the alone previous one that, before compressing, eliminates the jumps of line and spaces innescesarios, but we must bear in mind the same precautions or it will not work.
I have developed this compression method for ProgramacionWeb, as you will be able comprovar on having seen the code source of the same page, the whole code appears without line jumps not tabulated by a simple compression question.
Eloi of St Martin Lagranje
http://www.programacionweb.net/articulos/articulo/?num=162 |