Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,345
    Thanks
    4
    Thanked 482 Times in 470 Posts

    gzip without headaches

    One of the more annoying things with PHP is getting it to allow you to gzip compress your PHP output. While there are many techniques the one I've come to favor is pretty simple.

    In your PHP file you want to add compression to, just add this at the start of your very first PHP file.

    Code:
    define('CONTENT_ENCODING', 
    	(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false) ? 'x-gzip' :
    	(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) ? 'gzip' :
    	false
    );
    if (CONTENT_ENCODING) include('gzip.lib.php');
    with this as your gzip.lib.php

    Code:
    <?php
    /*
    	gzip.lib.php
    	
    	Version 1.0 Jason M. Knight, August 2009
    	
    	Uses a proper exit handler to provide automation of gzip compression of our
    	PHP output with little if any headaches.
    	
    	ASSUMES:
    		CONTENT_ENCODING contains either 'x-gzip' or 'gzip' based on the value in
    		HTTP_ACCEPT_ENCODING. See "defines.php" to see how this is set.
    		
    	If STRIP_WHITESPACE is defined whitespace between tags or at the start of
    	lines will be stripped, as will comments. Whitespace between a tag and
    	CDATA or between attributes will be left alone.
    	
    */
    
    ob_start();
    ob_implicit_flush(0);
    register_shutdown_function(function() { 
    	header('Content-Encoding: ' . CONTENT_ENCODING);
    	$contents = ob_get_contents();
    	if (defined('STRIP_WHITESPACE')) $contents = preg_replace(
    		['#<!--.*?-->#s', '#>\s+<#', '#\n\s+<#'],
    		['', '><', '<'],
    		$data
    	);
      ob_end_clean();
    	echo "\x1f\x8b\x08\x00\x00\x00\x00\x00",
    		substr(gzcompress($contents, 6), 0, -4);
    });
    Because it hooks onto register_shutdown_function it will still operate properly if you die/exit, and you don't have to worry about calling anything special at the end of the code to handle that output.

    I've used it for quite some time, never had a problem with it. The whitespace stripping SHOULD ignore content of tags like <pre>, but I leave it disabled by default. It's just nice to have the option.

    Dunno if anyone else will find it handy, but thought I'd share something in here.

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,345
    Thanks
    4
    Thanked 482 Times in 470 Posts
    Oh, and if anyone has suggestions/improvements, I'd love to hear it. As you can see I've not made any real changes to it in 7 years -- and have used it on all my sites over that time.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •