Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Controlling PHP Output: Caching and compressing dynamic pages
Using Output Control to compress PHP output
You need the Zlib extension compiled in PHP4 to compress output. If needed, see the Zlib extension in the PHP documentation for install instructions.
First of all, initialize output buffering:

<?php

ob_start
();
ob_implicit_flush(0);

?>
Then, generate all the content using print, echo, or whatever you want. For example:

<?php

print("Hey this is a compressed output!");

?>
After the page is generated, we go back to the output using:

<?php

$contents
= ob_get_contents();
ob_end_clean();

?>
Then, we have to check if the browser supports compressed data. If so, the browser sends a ACCEPT_ENCODING HTTP header to the webserver in the request. We can check the variable $HTTP_ACCEPT_ENCODING and check for "gzip, deflate":

<?php

if(ereg('gzip, deflate',$HTTP_ACCEPT_ENCODING)) {
    
// Generation of Gzipped content
} else {
    echo
$contents;
}

?>
That's simple, structured and clean enough to use. Let's see how we generate gzipped output:
(Taken from php.net)

<?php

// Tell the browser that they are going to get gzip data
// Of course, you already checked if they support gzip or x-gzip
// and if they support x-gzip, you'd change the header to say
// x-gzip instead, right?
header("Content-Encoding: gzip");

// Display the header of the gzip file
// Thanks ck@medienkombinat.de!
// Only display this once
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";

// Figure out the size and CRC of the original for later
$Size = strlen($contents);
$Crc = crc32($contents);

// Compress the data
$contents = gzcompress($contents, 9);

// We can't just output it here, since the CRC is messed up.
// If I try to "echo $contents" at this point, the compressed
// data is sent, but not completely. There are four bytes at
// the end that are a CRC. Three are sent. The last one is
// left in limbo. Also, if we "echo $contents", then the next
// byte we echo will not be sent to the client. I am not sure
// if this is a bug in 4.0.2 or not, but the best way to avoid
// this is to put the correct CRC at the end of the compressed
// data. (The one generated by gzcompress looks WAY wrong.)
// This will stop Opera from crashing, gunzip will work, and
// other browsers won't keep loading indefinately.
//
// Strip off the old CRC (it's there, but it won't be displayed
// all the way -- very odd)
$contents = substr($contents, 0, strlen($contents) - 4);

// Show only the compressed data
echo $contents;

// Output the CRC, then the size of the original
gzip_PrintFourChars($Crc);
gzip_PrintFourChars($Size);

// Done. You can append further data by gzcompressing
// another string and reworking the CRC and Size stuff for
// it too. Repeat until done.

function gzip_PrintFourChars($Val) {
    for (
$i = 0; $i < 4; $i ++) {
        echo
chr($Val % 256);
        
$Val = floor($Val / 256);
    }
}

?>
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
Check this firstEdemilson Lima08/20/03 02:03
How to test the effect?Gopi11/26/02 08:07
include_once much faster than readfileAnthony Topper08/02/02 10:32
ob_gzhandler returns nothing!AtLAnTiS07/19/02 03:57
RE: Garbage displayedParker07/01/02 14:52
RE: Garbage displayedtobias deutsch06/28/02 10:02
RE: will google (and others) crawl gzip content?Hannes Schmiderer04/20/02 04:19
will google (and others) crawl gzip content?Benjamin Kuz04/15/02 05:20
Garbage displayedMatt01/20/02 17:33
script changesredHairedBitch12/20/01 08:29
Output buffering not workingJonathan09/05/01 21:20
RE: Compression: true or false??Gossip08/17/01 06:19
RE: why compress when its already compressed?terry chay08/02/01 18:32
RE: Problems with session IDterry chay08/02/01 18:20
RE: much easier solutionterry chay08/02/01 18:15
RE: Compression: true or false??terry chay08/02/01 18:08
RE: Compression: true or false??Colin07/06/01 16:15
RE: win32Daniel Gustafsson06/30/01 11:28
RE: Chill OutJeff C06/02/01 13:07
RE: much easier solutionJeff C06/02/01 09:36
Compression: true or false??Barefoot05/30/01 04:48
RE: why compress when its already compressed?Monte Ohrt05/08/01 11:12
RE: win32Mark Walker05/03/01 13:20
RE: why compress when its already compressed?Mark Walker05/03/01 13:18
RE: Fred - How to test the effect ?Sonny Savage05/02/01 13:11
RE: why compress when its already compressed?DB765432103/18/01 23:13
RE: How to test the effect ?Fred03/12/01 14:13
Server limitations....Eric Nielsen03/08/01 15:17
Chill OutKevin J. Menard, Jr.03/08/01 13:10
much easier solutionLordBlink03/07/01 13:33
RE: Ignore last message / ob_gzhandler fix in CVSJason02/28/01 08:01
RE: Ignore last message / ob_gzhandler fix in CVSJason02/28/01 07:54
RE: Ignore last message / ob_gzhandler fix in CVSJason02/28/01 07:41
Problems with session IDAndrew02/28/01 07:10
why compress when its already compressed?Thamir Ghaslaan02/25/01 19:29
RE: mod_gzip 1.3.17aMichael Douma02/22/01 17:56
Class for this type of cachingChristian Stocker02/22/01 03:36
RE: mod_gzip 1.3.17aDB765432102/19/01 12:27
mod_gzip 1.3.17aTim Frank02/16/01 20:15
Ignore last message / ob_gzhandler fix in CVSTim Kask02/09/01 02:32
RE: Strange Effect: Takes entire server downTim Kask02/09/01 01:46
RE: Where to install Zlib?Rik Bradt02/08/01 03:09
Need help determine compression gainWill02/07/01 09:13
RE: How to test the effect ?terry chay02/07/01 07:06
Important noteLuis Argerich02/07/01 06:31
RE: win32Will02/06/01 03:44
RE: How to test the effect ?Will02/06/01 03:42
win32Kreft02/04/01 19:10
How to test the effect ?Alain Fontaine02/04/01 04:47
RE: This works much better ...Tim Frank02/02/01 21:51
PHP4 snapshot (2/1/01) appears okayDerek Piper02/02/01 14:42
This works much better ...Sebastian Benner02/02/01 04:33
RE: Mod_Gzipcolin02/01/01 12:25
RE: Strange Effect: Takes entire server downTim Perdue, PHPBuilder.com02/01/01 09:24
Where to install Zlib?Will02/01/01 08:46
Mod_GzipManu02/01/01 05:00
RE: Isn't this supported by the latest Mod Gzip?UnKn0wN01/31/01 15:17
RE: Browsers?terry chay01/31/01 10:55
RE: Why? it's better and easy to use apcterry chay01/31/01 10:31
PHP ob_gzhandler memory leakDerek Piper01/31/01 08:46
problems with built-in output compressingHenning Peters01/31/01 06:52
Very good class with _VARIABLE_ compression.Will01/30/01 07:23
RE: Browsers?Will01/30/01 07:18
Cool IE 5 behaviourAndrew Coldham01/30/01 03:01
RE: The zend solution did not work for me.Andrew Coldham01/30/01 02:49
Strange Effect: Takes entire server downTom Anderson01/30/01 01:42
Problem with Windows 98 & IE 5?Justin 01/29/01 21:45
The zend solution did not work for me.Eric Naujock01/29/01 18:21
Ignore my questionChris01/29/01 16:12
RE: Much better solution here :Chris01/29/01 16:11
Browsers?Philip Hofstetter01/29/01 12:01
RE: Isn't this supported by the latest Mod Gzip?Ivan01/29/01 09:57
Why? it's better and easy to use apcmiker01/29/01 06:07
Any strange effects with this?terry chay01/28/01 07:14
Output Buffering, from the source.Andrew Coldham01/27/01 21:02
Incredible!Jeff01/27/01 11:42
RE: HeadersDraxx01/27/01 08:51
HeadersJeff01/26/01 15:58
Nice idea, but Debian users bewareMathew Binkley01/26/01 11:42
RE: Sourceforge (Much better solution here)UnKn0wN01/26/01 11:25
RE: Sourceforge (Much better solution here)Tim Perdue, PHPBuilder.com01/26/01 10:04
RE: Much better solution here :Tim Perdue, PHPBuilder.com01/26/01 10:03
RE: Much better solution here :Derek Piper01/26/01 09:53
A complete cache validator is uploadedRicardo Galli01/26/01 05:50
RE: Much better solution here :Romain01/26/01 03:32
RE: Sourceforge (Much better solution here)Jonathan Ragan-Kelley01/25/01 20:41
RE: Much better solution here :Tom Anderson01/25/01 18:54
RE: Much better solution here :Luis Argerich01/25/01 17:38
RE: Much better solution here :Tim Perdue, PHPBuilder.com01/25/01 15:53
RE: Isn't this supported by the latest Mod Gzip?Derek Piper01/25/01 15:36
RE: Does this use up a lot of the server's power?Betcour01/25/01 11:03
Some new things....Luis Argerich01/25/01 08:30
RE: Isn't this supported by the latest Mod Gzip?Will01/25/01 08:14
way too much code!!!w00kie01/25/01 06:29
Does this use up a lot of the server's power?Björn Brändewall01/25/01 05:02
Much better solution here :Betcour01/25/01 04:20
Russian Apache and mod_gzipCyrill Malevanov01/25/01 03:54
cgi_buffer does this too, with e-tagsSteve Warwick01/25/01 03:36
RE: Isn't this supported by the latest Mod Gzip?Draxx01/25/01 03:30
Isn't this supported by the latest Mod Gzip?Matthew Villa01/24/01 23:15
a complete caching system for phpnathan01/24/01 22:58
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.