JohnOP Posted August 10, 2011 Share Posted August 10, 2011 I am nearly getting there to launch my website and before i do i want to make sure i have all the security, seo and optimization done to the website. Now i have been reading up on Gzip compressing my php files to make them smaller files on the fly which would make pages load faster. i found this function <?php 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(); } } ?> then on pages i would put <?php ob_start(); ob_implicit_flush(0); //content print_gzipped_page(); But i am out putting the file size to a test page to test this and even with that function in the file size stays the same, could someone tell me how this works correctly? Quote Link to comment https://forums.phpfreaks.com/topic/244378-php-gzip/ Share on other sites More sharing options...
Jumpy09 Posted August 10, 2011 Share Posted August 10, 2011 I personally use the htaccess bit I found for gzip compression, if you want to try it: <ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </ifModule> Just place that portion in your .htaccess in the root directory and it should control that bit automatically. I can't say with 100% what all of it does, but it does compress the content being sent out. Plus this way you don't have to worry about headers already being sent. It is up to you, if not then I do hope someone who can answer your question comes along. Edit: The mod_gzip_item_exclude mime ^image/.* prevents caching of images! I simply did this because I was going to use a non-cache system for images although I decided to cache the images. If you want to cache images just change the exclude on that to include! Quote Link to comment https://forums.phpfreaks.com/topic/244378-php-gzip/#findComment-1255149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.