PHP / MySQL site using caching. What does this work most of the time?

Our previous webmaster created this site, and its caching works fine for most browsers, but I found that some versions of Opera do not work, and some online SEO tools like Send Express Analyzer . I suspect this is a problem with the headers. I read about ob_start ("ob_gzhandler"), but I'm not sure how I can implement it in this scenario.

The entire site is managed by a router file, unique controller files for each page type, and .htaccess. The code below is in the router file to search for the cached version of the page and load the contents, otherwise load the page.

Caching compresses the contents of the page using gzencode and stores it in the database in the cache table with the uri hash code. If the cached file exists, the contents are retrieved from the database.

Opera displays a blank page, but the Express Express method does not recognize the page. I am pretty sure that the .htaccess file has nothing to do with this problem. Below is the code where I think the problem is.

<?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... { 

Bad 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 is a lot going on in this file, and I tried to cut as many things as possible in order to eliminate the confusion. Any help is certainly greatly appreciated! Thanks in advance!

+4
source share
2 answers

Well, this one looks bad.

First of all, error handling is terrible - swallowing such errors in the code section called on each page sucks.

Secondly, most web servers will handle gzip, etc. pretty much out of the box - there really is no reason to invent it yourself.

Thirdly, it may be due to the way you split the code, but I think that in the “IF” block below there is no case for echo content if the current browser does not accept compressed content.

 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; } } 
0
source

There are all sorts of factors, but most of the time, if there is excess garbage in the compressed version sent to the browser, and some cannot expand the page with this material in them.

Usually at the top there are only a few lines, E_NOTICES (use error_repoting(0); to suppress) and, possibly, even the encoding used in the storage tables. Other than that, it could just be an outdated compression extension for PHP, and Opera uses this method.

I'm not sure if file_get_contents('...') is retrieving the gzipped version, but if it is not, open the site in Opera and use something like WireShark to see what the browser gets exactly.

0
source

Source: https://habr.com/ru/post/1402693/


All Articles