Forum Moderators: coopster
I check gzip status using this program...
[leknor.com...]
ob_start();
ob_implicit_flush(0);
// your script goes here
$contents = ob_get_contents();
ob_end_clean();
$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.
Andreas
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.
Andreas
$ACCEPT_ENCODING = $_SERVER['HTTP_ACCEPT_ENCODING'];
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.
Andreas
It is really easy.
<?phpPacking and Caching
/*
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';
Check whether the UA sending the request supports gzip.
$ACCEPT_ENCODING = $_SERVER['HTTP_ACCEPT_ENCODING'];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.
if (strpos($ACCEPT_ENCODING, 'gzip')!== false) $enc = 'gzip';
if (strpos($ACCEPT_ENCODING, 'x-gzip')!== false) $enc = 'x-gzip';
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/'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.
. md5(implode('',
array($_SERVER['REQUEST_URI'],
$sessionid,
$QUERY_STRING,
$theme,
($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'),
$page,
$desired_language,
))) . '_' . $enc;
if (count($_COOKIE)>0If-Modified Handling
or $_SERVER['REQUEST_METHOD']=='POST'
or (!is_valid($cached_file))
or ($info = @stat($cached_file)) == false
) {
/* If there's no file or it's invalid we generate the output */
ob_start();
ob_implicit_flush(0);
#
include($file);
#
write_contents2cache($cached_file, $enc);
}
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 noNow that we are sure a valid output file exists we send the appropriate header fields and then the actual file.
changes since last request */
$last = strtotime($info['mtime']);
$cond = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])?
$_SERVER['HTTP_IF_MODIFIED_SINCE'] : 0;
#
if ($cond and $_SERVER['REQUEST_METHOD'] == 'GET'
and strtotime($cond) >= $last) {
header('HTTP/1.0 304 Not Modified');
exit;
}
/* Output the file now that we are sure the file exists */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 ($enc) {
header("Content-Encoding: $enc");
$size = filesize($cached_file);
header("Content-Length: $size");
}
#
readfile($cached_file);
if (isset($sessionid)
or isset($_COOKIE['authorid'])
or $_SERVER['REQUEST_METHOD']=='POST'
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.
Andreas