Our previous webmaster set up this site and the caching he set up works fine for most browsers but I have found that some versions of Opera don’t work and some online SEO tools like Submit Express Analyzer. I suspect that it’s an issue with headers. I’ve read about ob_start(“ob_gzhandler”) but I’m not sure how I can implement it in this scenario.
The site is all controlled by a router file, unique controller files for each type of page and .htaccess. In the router file, the code below is set in place to look for a cached version of the page and load the content, otherwise, load the page.
The caching compresses the page content with gzencode and stores it in the database in a cache table with a uri hash. If the cached file exists, the content is pulled from the database.
Opera displays a blank page and the Submit Express Test does not recognize a page. I am almost certain that the .htaccess file has nothing to do with the issue. The code below is where I think the issue resides.
<?php
$loadTime = microtime(true);
session_start();
if (!isset($_SESSION['var']))
$_SESSION['var'] = rand(0, 2);
if (!isset($_SESSION['var2']))
$_SESSION['var2'] = rand(0, 4);
require(dirname(__FILE__).'/config/common.php');
$uri = $_SERVER['REQUEST_URI'];
$request = explode('/', substr($uri, 1));
$request = preg_replace('/\..*/', '', $request);
$uriHash = sha1($uri);
if($uri == '/') //This is the Index...
{
Irrelevant Index Code
}
try
{ // LOOKS FOR CACHE IN DB BASED ON URI
$cache = $GLOBALS['db']->getRow("SELECT * FROM cache_tbl WHERE uri_hash = '$uriHash'");
}catch ( Exception $e ) {
$cache = array();
}
if ( !empty($cache) && ($cache['mod_date'] * 60 * 60 * 24) > $loadTime )
{ // IF NO CACHE IN DB, SETS HEADERS FOR COMPRESS OR NO COMPRESS
$HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
if( headers_sent() )
$encoding = false;
else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
$encoding = 'x-gzip';
else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
$encoding = 'gzip';
else
$encoding = false;
$compressed = $cache['contents'];
if ( $encoding ) {
header("Content-Encoding: ".$encoding);
echo $compressed;
}
} else {
if ($request[0] == 'venues') {
header("HTTP/1.1 301 Moved Permanently");
header('Location: '.HOST.$request[1].'.html');
}
More Code
if ( // Irrelevant Conditions ) {
$contents = ob_get_clean();
echo $contents;
$compressed = gzencode($contents, 7);
try {
Admin::add('cache_tbl', array('uri_hash' => $uriHash, 'contents' => $compressed, 'mod_date' => $loadTime));
} catch(Exception $e) { ; }
ob_end_flush();
}
}
There’s a lot going on in this file and I tried to cut out as much stuff as possible to eliminate confusion. Any help is obviously greatly appreciated! Thanks in advance!
Okay, this one looks nasty.
Firstly, the error handling is awful – swallowing errors like that in a code section that gets called on every page sucks.
Secondly, most web servers will handle GZIP etc. pretty much out of the box – there’s really no reason to invent this yourself.
Thirdly, it may be because of the way you’ve split up the code, but I think that in the “IF” block below, there’s no case for echoing out the content if the current browser does not accept compressed content.