PHP program accelerated exploration [7] - Compression output Gzip

xiaoxiao2021-03-06  96

◆ Compressed output Gzip

With the mod_gzip module in Apache, we can use Gzip's compression algorithm to compress the web content published by the Apache server and then transfer to the client's browser. If it is the content of plain text, the effect is very obvious, about 30% -40% of the original, and the user's browsing speed is greatly accelerated.

Gzip requires client browser support, and most browsers currently support GZIP, such as IE, Netscape, Mozilla, etc., so this method is worth a try. We can use predefined variables in php $ _SERVER ['http_accept_encoding'] to determine if the client browser supports Gzip.

gzip1.php

IF (EREG ('gzip', $ _ server ['http_accept_encoding'])) {??? // browser support} Else {??? // browser does not support, output other content}?>

Next, we extends the above PHP program, use ob_start (ob_gzhandler) to compress the contents of the webpage, deposit into the buffer and send the Gzip's browser, and the browser will automatically decompress the compressed content, display.

gzip2.php

Define ('max "; error (EREG (' gzip ', $ _ server [' http_accept_encoding ']) {??? // browser supports Gzip, compress content and buffer output ???? OB_Start (" ob_gzhandler "); ???? $ output = ''; ???? for ($ I = 0; $ I <= max; $ i ) ???? {??????? $ OUTPUT. = "This is line $ i"; ????} ???? echo "browser supports Gzip compression output"; ???? echo $ output;} else {???? // browser does not support, Direct output ???? for ($ I = 0; $ I <= max; $ i ) ???? {??????? $ output. = "This is line $ I";??? ?} ???? ???? echo "browser does not support GZIP compression output"; ???? echo $ output;}?>

?

HTTP header information using Gzip compression generated web pages will be more such information compared to a general web page:

Content-encoding: Gzip

Content-Length: 270

?

If you want more detailed information, please see the MOD_GZIP project home page:

http://sourceforge.net/projects/mod-gzip/

?

Similarly, we can also utilize mod_deflate, compression ratio is slightly lower than mod_gzip. Calling the zip function requires the server memory, so it is necessary to use it, depending on the demand.

转载请注明原文地址:https://www.9cbs.com/read-123769.html

New Post(0)