Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Cache system problems.

    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:
    PHP 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;
            @
    clearstatcache();

            if (
    time() - $this->cachetime $cachefile_created
            {
                
    //ob_start('ob_gzhandler');
                
    @readfile($this->cachefile);
                
    //ob_end_flush();
                
    exit();
            }
            else
            {
                
    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.
            
    else
                
    $this->page ENCRYPT('http://' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // Requested page

            
    $this->cachefile $this->cachedir $this->append '/' $this->page '_' $this->permixprepend '.' $this->cacheext
            
            
    unlink($this->cachefile);
        }
        
        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);
                    }
                }
                @
    closedir($handle);
            }
        }
        
        public function 
    CacheStop()
        {
            
    $fp = @fopen($this->cachefile'w');

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

            
    ob_end_flush();
        }
        
        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:
    PHP Code:

    $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;
        
    $ip $_SERVER['REMOTE_ADDR'];
        
    $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."'");
        
        if(
    CACHE_ON)
        {
            
    $cache->ResetPageCache();
            
    //$cache->ResetPageCache('/profiles/profile.php?user='.$_SESSION['user']->username.'');
        
    }
        
        
    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');
    $form->setRequiredNote('');

    if (
    $form->validate()) 
    {
        
    $form->freeze();
        
    $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...)
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Where is this class? (HTML_QuickForm).
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    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:
    http://pear.php.net/package/HTML_QuickForm/



    [edit]
    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.
    Last edited by Digicoder; 03-11-2008 at 10:42 AM.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #4
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Hahahahaha!!!

    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.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •