Compression of the data on PHP
Many browsers podderzhyvajut gzip the compressed files in a "transparent" mode for the user.
For text files (html pages) koeficient compression by this method makes 0.2-0.3,
I.e. the data are compressed in 3-5 times (on the big files - more), that gives the same acceleration zakachki page under other equal conditions.
That reduces ehfektivnost` uses of compression of the data
Efekt from compression it is reduced, if you use many{a lot of;much} schedules (gif, jpeg, png), flash (swf), other multimedia.
But if at you text and KHTML the page borrows{occupies} the majority of the data more 50kb - safely use compression.
Efekt from use of compression it is reduced also at use of modern modem reports.
That their manufacturers speak:
Reports of compression of the data (v.44/v.42 bis/mnp 5) and corrections of mistakes (v.42/mnp 2-4) are supported for maximization of bandwidth and check of integrity of transfer. v.44 more ehfektivnyj a method of compression than v.42 bis, that essentially increases returnable bandwidth and thus, reducing time zakachki for types of the files used in the Internet, such kake Webs - pages and not compressed the schedule, audio, documents. Compression under the report v.44 can reach{achieve} a level more than on 25 % ehfektivnee than v.42bis. Typical koeficient compression for v.44 on Webs - pages about{near} 6-1, that finally gives bandwidth up to 300 kbps for 56-kbps connections.
Therefore ehfekt in time of loading can be not absolutely appreciable. Nevertheless use of compression of the data is justified. First, algorithm gzip more ehfektivnyj aparatnogo compression of the data (he on 17 % concedes all to a rar-format and on 2 % - zip), in the most part due to that you compress all file zarazom, and the modem - only the current packages. Second, total bandwidth of the channel vsegad is less most than his{its} "thin" place i.e. if on what that stages is used older report, ehfekta from aparatnogo the end user will not receive compression.
How to learn{find out}, whether supports a browser compression
If the variable of an environment http_accept_encoding contains "gzip" or "x-gzip", means supports. To learn{find out} about it in php it is possible having checked up a variable $http_server_vars ['http_accept_encoding'] on ocurrence "gzip";
php has the built - in functions of compression?
To use compression, PKHP should be compiled with library zlib. Her{it} mozhna to download here: http://www.gzip.org/zlib, but there is she and in the distribution kit php. At start "configure" it is necessary to specify parameter-with-zlib or-with-zlib =/path/to/zlib (If you use byblioteku not from the distribution kit). Users windows should register in php.ini "extension=php_zlib.dll" (Naturally, php_zlib.dll should be in a folder specified in parameter "extension_dir=c:phpextensions")
As far as quickly works funckcija compression?
On the processor p166mmx gzencode compresses about{near} 600-700 kb in a second
How to compress page
For compression of page it is possible to use two ways:
All conclusion to do{make} not through echo, and in a separate variable.
All conclusion to do{make} in the buffer of a conclusion, having put in the beginning of page
<? php
ob_start (); // buffering of a conclusion
ob_implicit_flush (0); // delivery of the buffer only on ob_end_flush command ()
?>
When the conclusion of page is ended, we receive contents of the buffer through
<? php
$page = ob_get_contents ();
?> also it is compressed by his{its} function
<? php
$page = gzencode ($page);
?> Before a conclusion of the compressed page it is necessary to send correct headings
<? php
header (' content-encoding: gzip ');
header (' vary: accept-encoding ');
header (' content-length: '. strlen ($page));
?> Certainly it is possible to do{make} all this by handles, and it is possible and to use classes pear "http_compress" i "cache_outputcompression". And it is even easier to save to itself this file:
zip.php
<? php
function start () {
ob_start ();
ob_implicit_flush (0);
}
function output ($compress = true, $use_etag = true, $send_body = true) {
$min_gz_size = 1024;
$page = ob_get_contents ();
$length = strlen ($page);
ob_end_clean ();
if ($compress ** extension_loaded ('zlib') ** (strlen ($page)> $min_gz_size) ** isset ($globals ['http_server_vars'] ['http_accept_encoding '])) {
$ae = explode (',', str_replace ('', ", $globals [' http_server_vars'] ['http_accept_encoding ']));
$enc = false;
if (in_array ('gzip', $ae)) {
$enc = 'gzip';
} else if (in_array ('x-gzip', $ae)) {
$enc = 'x-gzip';
}
if ($enc) {
$length = strlen ($page);
header (' content-encoding: '. $enc);
header (' vary: accept-encoding ');
} else {
$compress = false;
}
} else {
$compress = false;
}
if ($use_etag) {
$etag = '"'. md5 ($page.) '"';
header (' etag: '. $etag);
if (isset ($globals ['http_server_vars'] ['http_if_none_match '])) {
$inm = explode (',', $globals ['http_server_vars'] ['http_if_none_match ']);
foreach ($inm as $i) {
if (trim ($i) == $etag) {
header (' http/1.0 304 not modified ');
$send_body = false;
break;
}
}
}
}
if ($send_body) {
header (' content-length: '. $length);
echo $page;
}
}
?> and to include it{him} in the necessary pages:
<? php
include ('zip.php');
start ();
/*
ctranica
*/
output ();
?>

|