homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

gziped my own php programs?

 7:43 pm on Nov 3, 2002 (gmt 0)

I have recomplied PHP for my server, and I was wondering how to enable gzip for my own php programs? Is there some script I have to attach to my existing programs to do this task?

I check gzip status using this program...




 8:23 pm on Nov 3, 2002 (gmt 0)

Sending the output of your php scripts gzipped is fairly easy. Just enable output buffering, do whatever your script does just as you did before, then get your scripts output and pack it.

// your script goes here
$contents = ob_get_contents();
$contents = gzencode($contents, 9);

Be sure to use a fairly new version of php since there used to be problems with gzencode in earlier releases.



 8:53 pm on Nov 3, 2002 (gmt 0)

Thanks. Do you know why this script returns a blank page on Netscape 7?

I notice some websites using gzip also suffer from this 'blank page' problem. Among them are SitePoint.


 12:55 am on Nov 4, 2002 (gmt 0)

I believe this to be due to the aforementioned problems with gzencode. The following code should do the trick:

if (phpversion() >= '4.2') { 
$contents = gzencode($contents, 9);
} else {
$gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
$size = strlen($contents);
$crc = crc32($contents);
$gzdata .= gzcompress($contents, 9);
$gzdata = substr($gzdata, 0, strlen($gzdata) - 4);
$gzdata .= pack("V",$crc) . pack("V", $size);
$contents = $gzdata;

BTW I donīt know whether said problem was fixed with version 4.2 (Perhaps php.net would help?). However I run one server with PHP 4.1.2 which has the empty page problem when using gzencode and one running PHP 4.2.3 which works just fine. The above code works on both servers.



 1:08 am on Nov 4, 2002 (gmt 0)

Will turning on gzip php affect google spidering? Will Google see a blank page in some rare situations where ob_start is acting up.


 1:21 am on Nov 4, 2002 (gmt 0)

You need to make sure to send the gzipped output only to those UAs that indicate that they accept it. Check the Accept-Encoding field in the HTTP request header.

if (strpos($ACCEPT_ENCODING, 'gzip')!== false) $enc = 'gzip';
if (strpos($ACCEPT_ENCODING, 'x-gzip')!== false) $enc = 'x-gzip';

I donīt know whether Google accepts gzipped output. I would think they donīt since that would increase the load on their servers.



 2:03 am on Nov 4, 2002 (gmt 0)

BTW if you are implementing sending gziped output it might be a good time to think about implementing caching and 304 Not Modified handling as well.

It is really easy.

A simple caching, zipping and If-Modified
handling script by Andreas Friedrich
$Id: cache.php,v 1.1 2002/07/16 14:45:06 af Exp $
$Name: $
$WEB_DEV_CACHE_DIR = '/var/www/html/www.ecotur.de//cache';
Packing and Caching

Check whether the UA sending the request supports gzip.

if (strpos($ACCEPT_ENCODING, 'gzip')!== false) $enc = 'gzip';
if (strpos($ACCEPT_ENCODING, 'x-gzip')!== false) $enc = 'x-gzip';
Build a unique filename for the requested file. You can use caching for slightly dynamic pages as well. Just include the variables that determine what is output in the array below.

Imagine a site that serves output depending on the UA string in the HTTP request header. For requests to a given URL we would need to store a file for each different UA in our cache.

$cached_file = '/var/www/html/www.ecotur.de//cache/' 
. md5(implode('',
($sm? 'true' : 'fals'),
($ns4? 'true' : 'fals'),
($ns6? 'true' : 'fals'),
($moz? 'true' : 'fals'),
($op? 'true' : 'fals'),
($ie? 'true' : 'fals'),
($ie4? 'true' : 'fals'),
($ie5? 'true' : 'fals'),
($ie6? 'true' : 'fals'),
($linux? 'true' : 'fals'),
($mac? 'true' : 'fals'),
))) . '_' . $enc;
If the response needs to be handled by the main script as is the case if the UA send a cookie or posted some data or the cached file is invalid or does not exist we start output buffering and run the script. Then we write the output to disk.

if (count($_COOKIE)>0 
or (!is_valid($cached_file))
or ($info = @stat($cached_file)) == false
) {
/* If there's no file or it's invalid we generate the output */
write_contents2cache($cached_file, $enc);
If-Modified Handling

If the UA sent a conditional request we check whether the cached file was changed since the time the UA supplied. If not we return a 'HTTP/1.0 304 Not Modified' status and exit the script.

/* Send 304 Not Modified if there were no  
changes since last request */
$last = strtotime($info['mtime']);
$cond = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])?
if ($cond and $_SERVER['REQUEST_METHOD'] == 'GET'
and strtotime($cond) >= $last) {
header('HTTP/1.0 304 Not Modified');
Now that we are sure a valid output file exists we send the appropriate header fields and then the actual file.

/* Output the file now that we are sure the file exists */ 
if ($enc) {
header("Content-Encoding: $enc");
$size = filesize($cached_file);
header("Content-Length: $size");
After sending the file we decide whether we want to keep the cached version or not. If it is highly dynamic (.i.e. uses cookies or posted data or contains a session id) we delete it.

if (isset($sessionid) 
or isset($_COOKIE['authorid'])
or count($_COOKIE)>0) @unlink($cached_file);
if ($local) @unlink($cached_file);

It might be better to save the file to disk only after we decide we want to keep it. This will eliminate the time to access the hd and write and delete the file.

Hope this helps and is useful.



 11:02 am on Nov 4, 2002 (gmt 0)

In PHP >= 4.0.4 you can use


It also supports deflated encoding and will check the HTTP headers for you, returning non-compressed pages to browsers which don't support it.


 2:53 pm on Nov 4, 2002 (gmt 0)

And you will suffer from the same problems as using gzencode in PHP versions prior to 4.2.



 4:48 pm on Nov 4, 2002 (gmt 0)

My host is PHP 4.2.3 and I haven't seen any problems.


 6:10 pm on Nov 4, 2002 (gmt 0)

Exactly. 4.2.3 is a later version than 4.2. So everything is working ok. The problem exists with servers running PHP versions prior to 4.2.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved