About gzip

Discussion in 'PHP' started by innovativewebart, Apr 22, 2013.

  1. #1
    Hello,
    I need to compressed my website's code because Its taking so much time for uploading so I want to know about the gzip.
    Anybody can help me how I can implement gzip for my website.

    Thanks in advance
    with Gratitude
    Rinks
     
    Solved! View solution.
    innovativewebart, Apr 22, 2013 IP
  2. #2
    You should edit (or create) .htaccess file in your root directory with following content:
    # BEGIN GZIP
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
    </ifmodule>
    # END GZIP
    Code (markup):
    Also this should be helpful:
    ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/html "access 8 hours"
    ExpiresByType text/css "access 8 hours"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 7 days"
    </IfModule>
    ## EXPIRES CACHING ##
    Code (markup):
     
    ntmedia, Apr 22, 2013 IP
  3. innovativewebart

    innovativewebart Greenhorn

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #3
    Hello NT,
    Thank you very much for your suggestion.I am waiting for another answer after that I will choose you as a best answer.....:).
     
    innovativewebart, Apr 22, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    The above is great for static files, but it does jack for your PHP -- since you posted this in PHP, I figure that's what you're asking about; gzipping your code output?

    Before you output ANYTHING from your PHP, do this:
    if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'x-gzip')!==false) {
    	$compressionType='x-gzip';
    	ob_start();
    } else if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')!==false) {
    	$compressionType='gzip';
    	ob_start();
    } else $compressionType = '';
    Code (markup):
    ... and before you exit, you'll want to do this:
    if ($compressionType) {
    	if (ob_get_length() < 2048) {
    		/* don't waste time compressing anything less than 2k */
    		ob_end_flush();
    	} else {
    		$contents=ob_get_contents();
    		ob_end_clean();
    		header('Content-Encoding: '.$compressionType);
    		print("\x1F\x8B\x08\x00\x00\x00\x00\x00");
    		$contents=gzcompress($contents,$data->settings['compressionLevel']);
    		/* strip off faulty 4 digit CRC when printing */
    		print(substr($contents,0,strlen($contents)-4));
    	}
    }
    Code (markup):
    ... and that's how you compress the PHP output. Mind you if you're already playing games with the outputbuffer you really can't do this. I actually put these in my template_header and template_footer functions since in my code the templates are the only thing allowed to output... uhm... anything.
     
    deathshadow, Apr 24, 2013 IP