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
mod_gzip is an Apache module which compresses static html pages using Gzip, according to IETF standards for browsers that accept gzip enconding (IE, Netscape, etc). mod_gzip may accelerate the download time for pages 4/5 times and I strongly suggest you use mod_gzip in your webserver. However, due to a lack of a filtering mechanism between modules in Apache 1.x.x, there is no way to compress PHP generated output using mod_gzip. Therefore, we have to build our own compressing engine in PHP. In this article, I will explain how to use PHP output controlling functions to make your pages load FAST!
Introducing PHP output control functions
One of the best things in PHP4 is that you can tell PHP to buffer all of the output generated in the script, so no content is sent to the browser, until you decide to send it. You can use this function to use header and setcookie functions, wherever you want in your script. However, this is only a small advantage of the powerful output functions.

<?php

void ob_start
(void);

?>
This is used to tell the PHP processor to redirect all the output to an internal buffer. No output will be sent to the browser after a call to ob_start.

<?php

string ob_get_contents
(void);

?>
This returns the output buffer in a string that you can echo to send the accumulated output to the browser (after turning buffering off!).

<?php

int ob_get_length
(void);

?>
This returns the length of the output buffer.

<?php

void ob_end_clean
(void);

?>
Cleans the output buffer and also turns output buffering off. You have to use this function before outputing content to the browser.
void ob_implicit_flush ([int flag])
Is used to turn on/off implicit flushing (default=off). If this is on, then a "flush" is executed for every print/echo or output command and output is immediately sent to the browser.
[ Next Page ]


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.