html compress issue
i am facing a problem w开发者_运维问答ith my server load. I checked all my queries and other stuff's but they are okey finaly i found the problem it comes from this function
function compress($buffer) {
$buffer = str_replace("\t", '', $buffer);
return $buffer;
}
ob_start("ob_gzhandler");
ob_start("compress");
when i remove that function the load is come to normal.
any idea's about this issue.
Regards
ob_start("ob_gzhandler");
This method is dependant on the Apache server and in addition, the mod_gzip module must be installed and loaded. While using the compression remember, the great thing that compression on the server activated only if browsers requests compressed content, in case the browser does not understand compressed content or does not request for it, the server simply servers plain, uncompressed content!
The method mentioned above is quick and easy, but the downfalls are that it only works on Apache with mod_gzip. Not only that, but according to the Php manual, that is not the preferred method for gzipping.
The compression level set to 6 by default. A higher compression level means more load on the server's CPU. You might want to experiment with the levels to find a value that will give you a good compression for an acceptable amount of load on the server.
To change the compression level, simply insert the following line before ob_start("ob_gzhandler"):
ini_set('zlib.output_compression_level', 4);
ob_start("ob_gzhandler"):
and another thing if you are using the PHP gzipping then remember to close the compression and if the gzipping is not closed then it sometimes cause the server load. Flush the output to the browser with ob_end_flush
ob_end_flush();
Recommandations usage of GZip connect:
Method 1:
If you are doing compression on all the pages then I would recommend using .htaccess method because it’s very simple to implement and by .htaccess file you can further configure server configurations. Now to enable gzip compression add the following line to your .htaccess file.
php_value output_handler ob_gzhandler
Method 2:
The proper way to use the Gzipping through PHP optimized way is:
// Include this function on your pages
function print_gzipped_page() {
global $HTTP_ACCEPT_ENCODING;
if( headers_sent() ){
$encoding = false;
}elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
$encoding = 'x-gzip';
}elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
$encoding = 'gzip';
}else{
$encoding = false;
}
if( $encoding ){
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
}else{
ob_end_flush();
exit();
}
}
// At the beginning of each page call these two functions
ob_start();
ob_implicit_flush(0);
// Then do everything you want to do on the page
echo 'Hello World';
// Call this function to output everything as gzipped content.
print_gzipped_page();
Hope this would resolve your server loading issue.
精彩评论