View Full Version : Cache system problems.

03-11-2008, 10:09 AM
Well, I'm not to sure how to explain this one.
I've worked on it for quite some time now and have come out with less of a clue than when I went in to do the deed in the first place.

The cache script I've built is rather large and quite a hassle to deal with, I'm posting a dumbed down version of it to remove security risks, as I'm sure those aren't the problem makers.
The Cache code:

class Cache
private $cachedir;
private $cachetime;
private $cacheext;
public $page;
public $cachefile;
private $permixprepend;
private $append;

public function __construct() // Build needed globals for class
// Settings
$this->cachedir = ''.getDir(/DIR/).'tmpcord/';
$this->cachetime = (60*60*12); // Seconds to cache files for. S,M,H
$this->cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)
$this->permixprepend = Cache::BuildPermixPrepend();
$this->append = Cache::FetchTemplateCache();

public function CachedPageCheck() // Check page, and output if needed

$this->page = ENCRYPT('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // Requested page
$this->cachefile = $this->cachedir . $this->append . '/' . $this->page . '_' . $this->permixprepend . '.' . $this->cacheext;

$cachefile_created = (@file_exists($this->cachefile)) ? @filemtime($this->cachefile) : 0;

if (time() - $this->cachetime < $cachefile_created)
ob_start(); // in case the page isn't created or it has timed up.

public function ResetPageCache($away=0) // reset a page bassed on name.
if($away != 0)
$this->page = ENCRYPT('http://' . $_SERVER['HTTP_HOST'] . $away); // Referal page.
$this->page = ENCRYPT('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // Requested page

$this->cachefile = $this->cachedir . $this->append . '/' . $this->page . '_' . $this->permixprepend . '.' . $this->cacheext;


public function PurgeCache() // This will clear all the sites cache, regardless of template or permissions.
if ($handle = @opendir($this->cachedir))
while (false !== ($file = @readdir($handle)))
if ($file != '.' and $file != '..')
@unlink($this->cachedir . $file);

public function CacheStop()
$fp = @fopen($this->cachefile, 'w');

// save the contents of output buffer to the file
@fwrite($fp, ob_get_contents());


private function FetchTemplateCache()
// Gets the users current template number, to better organize cache files in my system.
return $_SESSION['user']->template->cachenum;

private function BuildPermixPrepend()
//This generates a basic permissions set for the current user, so when the pages get cached other users don't see what they shouldn't.
return $permix;


There you have it. Now, that all works fine, but because I would rather not be asked to produce it later, it's here for reference.

Here is the snippet that I believe is somehow causing the problems:

$form = new HTML_QuickForm('Comment', 'POST', 'reviewspage.php?num='.$_GET['num'].''); // Name QF etc.

function comment_processing($data)
global $db;
global $cache;
$id = $_SESSION['user']->user_id;
$name = $_SESSION['user']->username;
$time = time();

$db->query("INSERT INTO site_revz_comments (rev_id, user_id, user_name, comment, user_ip, post_time)
VALUES('{$_GET['num']}', '$id', '$name', '{$data['comment_body']}', '$ip', '$time')");

$db->query("UPDATE site_revz SET comment_num=(comment_num + 1) WHERE rev_id='{$_GET['num']}'");
$db->query("UPDATE phpbb_users SET s_post=(s_post + 1) WHERE user_id='".$_SESSION['user']->user_id."'");


header('Location:'. $url . '/reviewspage.php?num='.$_GET['num']);

$form->addElement('UBBtextarea', 'comment_body', '', array('cols' => 80, 'rows' => 15));
$form->addElement('submit', 'submit', 'Submit');

$form->applyFilter('__ALL__', 'htmlentities');
$form->applyFilter('__ALL__', 'trim');
$form->applyFilter('__ALL__', 'escape');

$form->addRule('comment_body', 'You have to enter text.', 'required', null, 'client');

if ($form->validate())
$form->process('comment_processing', false);

Now that we have all the pieces it's time to explain the actual problem.
The cache system is working, and I only get this bug when some one submits a comment or some page content.
before you submit your comment you have the basic HTML_Quickform produced by the second snippet, it looks like this:

And once you post the comment, the page turns into this:

What I suspect, is that the HTML_QF freeze method is doing this. But one thing, the last thing in the comment processing is the call to purge the page cache and remove it so the new file can be made with the new comment and so forth.
Well what I don't get is how the comment submitted is turned into the actual comment field.
As you can see, when I could type into the text are I entered "test" and after the page processed the submission, added the comment, purged the cache, refreshed the page and made the new cache file with the new comment ("test") in place on the page, the comment text area turned into the actual comment text submission from the last submitted comment, thus rendering the form useless.

(I feel as If I'm babbling, sorry if it makes no sense. )

I can't seem to figure out why, nor how, this is happening. the caches is the last thing that happens before the page is refreshed allowing the new page to be made. But the thing is, this should happen if cache is off, should it not?

Thanks for the help on this guys. (I can't make sense of it and I'm sure I made less sense here than in my head, so ask me if you don't quite get it...)

03-11-2008, 10:16 AM
Where is this class? (HTML_QuickForm).

03-11-2008, 10:38 AM
Its a PEAR package, I'm using the depreciated version, but for my system it works the best.

you can find it and it's documentation here:

On the hole, this really really shouldn't be as hard as it seems...
I get the systems perfectly, I built one of them and have used the other for close to a year and a half now, I'm just stumped.
Two weeks of vacationing really got to me, I'm getting rusty again.

03-11-2008, 11:47 PM

I've got to say, late night anything can REALLY kill a guy.
Just looked at the code and found out what the problem was.

I didn't state and unfreeze in the process validation.
Since that's done, thanks _Aerospace_Eng_ for listening to my babble.