CodeIgniter Forums
Implementing some code - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Implementing some code (/thread-3181.html)



Implementing some code - El Forum - 09-14-2007

[eluser]manilodisan[/eluser]
I'm trying to implement what seems to be a very good class but I'm having big troubles. The code is supposed to help me cache the pages better and have some sort of a better control over the headers and stuff. The problem is that I have to use the code right after an ob_start() which should sit after the opening php tag and call the class right before the closing php tag. This would have been very simple on a regular php file but, as you can surely imagine, it's a little complicated with our controllers and views.

Here's a sample usage of the code:
Code:
<?php
//------------Start of file---------------

ob_start();            // <==== line 1
include('Conteg.inc'); // <==== line 2

//the page ...

new Conteg();          // <==== line 3
//-------------End of file----------------
?&gt;

Also, here's a link to the original package (it's on phpclasses.org you will need some sort of credentials), PLUS the original support page for this class with more details.


I would really appreciate your help.

Thank you.


Implementing some code - El Forum - 09-14-2007

[eluser]Michael Wales[/eluser]
Just curious why you don't use CI's caching? Caching is a pretty simple concept and one library isn't going to produce significant benefits over another.


Implementing some code - El Forum - 09-14-2007

[eluser]manilodisan[/eluser]
Well...I needed something more advanced with a little more options (Etag, Pragma etc.) and this seems to be the one. I'm just testing new water, might or might not give better results.


Implementing some code - El Forum - 09-14-2007

[eluser]Michael Wales[/eluser]
Open up the Caching library, basically copy-paste that into a new library of your own, make your code changes to use your new Caching library (you will see where the native library makes it's appropriate function calls).


Implementing some code - El Forum - 09-14-2007

[eluser]manilodisan[/eluser]
Well it's not that simple. I tried it with Output.php and Loader.php but it seems to fail at some point. On line 68 in Output.php (in CI's library folder) there's set_output() which seems to set the final output to be sent.

Code:
function set_output($output)
{
    $this->final_output = $output;
}


I tried working with that but I get errors of all types...


Implementing some code - El Forum - 09-14-2007

[eluser]Michael Wales[/eluser]
Experiment around line 172:

Code:
// Is compression requested?
if ($CFG->item('compress_output') === TRUE) {
    if (extension_loaded('zlib')) {
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
            ob_start('ob_gzhandler');
        }
    }
}

This is where CI is starting it's compression, which involves header changes - probably a good place for your to start modifying yours as well.


Implementing some code - El Forum - 09-14-2007

[eluser]Michael Wales[/eluser]
I have no idea why I thought Caching was an independent library... didn't have the code sitting in front of me. When you mentioned digging through Output and Loader I was like "wtf... I better look at what I am talking about"


Implementing some code - El Forum - 09-14-2007

[eluser]manilodisan[/eluser]
Yes...Caching is not separate on CI and far from what I would like. I tried this for the function that you recommended but no response besides a blank page and even on error_reporting(E_ALL);.

Code:
function _display($output = '')
    {    
        // Note:  We use globals because we can't use $CI =& get_instance()
        // since this function is sometimes called by the caching mechanism,
        // which happens before the CI super object is available.
        global $BM, $CFG;
        
        // --------------------------------------------------------------------
        
        // Set the output data
        if ($output == '')
        {
            $output =& $this->final_output;
        }
        
        // --------------------------------------------------------------------
        
        // Do we need to write a cache file?
        if ($this->cache_expiration > 0)
        {
            $this->_write_cache($output);
        }
        
        // --------------------------------------------------------------------

        // Parse out the elapsed time and memory usage,
        // then swap the pseudo-variables with the data
                
        $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');        
        $output = str_replace('{elapsed_time}', $elapsed, $output);
        
        $memory     = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
        $output = str_replace('{memory_usage}', $memory, $output);        

        // --------------------------------------------------------------------
        
        // Is compression requested?
        if ($CFG->item('compress_output') === TRUE)
        {
            if (extension_loaded('zlib'))
            {
                if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
                {
                    ob_start('ob_gzhandler');
                }
            }
        }
// --------------------------------------------------------------------
// --------------------------------------------------------------------
//                REQUIRE THE FILE THAT HOLDS THE CLASS
// --------------------------------------------------------------------
// --------------------------------------------------------------------
        require_once ( 'scripts/Cache.php' );
        // --------------------------------------------------------------------
        
        // Are there any server headers to send?
        if (count($this->headers) > 0)
        {
            foreach ($this->headers as $header)
            {
                @header($header);
            }
        }        

        // --------------------------------------------------------------------
        
        // Does the get_instance() function exist?
        // If not we know we are dealing with a cache file so we'll
        // simply echo out the data and exit.
        if ( ! function_exists('get_instance'))
        {
            echo $output;
            log_message('debug', "Final output sent to browser");
            log_message('debug', "Total execution time: ".$elapsed);
            return TRUE;
        }
    
        // --------------------------------------------------------------------

        // Grab the super object.  We'll need it in a moment...
        $CI =& get_instance();
        
        // Do we need to generate profile data?
        // If so, load the Profile class and run it.
        if ($this->enable_profiler == TRUE)
        {
            $CI->load->library('profiler');                
                                        
            // If the output data contains closing &lt;/body&gt; and &lt;/html&gt; tags
            // we will remove them and add them back after we insert the profile data
            if (preg_match("|&lt;/body&gt;.*?&lt;/html&gt;|is", $output))
            {
                $output  = preg_replace("|&lt;/body&gt;.*?&lt;/html&gt;|is", '', $output);
                $output .= $CI->profiler->run();
                $output .= '&lt;/body&gt;&lt;/html&gt;';
            }
            else
            {
                $output .= $CI->profiler->run();
            }
        }
        
        // --------------------------------------------------------------------

        // Does the controller contain a function named _output()?
        // If so send the output there.  Otherwise, echo it.
        
        if (method_exists($CI, '_output'))
        {
            $CI->_output($output);
        }
        else
        {
            echo $output;  // Send it to the browser!
        }
// --------------------------------------------------------------------
// --------------------------------------------------------------------
//                CALL THE CACHING CLASS
// --------------------------------------------------------------------
// --------------------------------------------------------------------
        new Conteg();
        log_message('debug', "Final output sent to browser");
        log_message('debug', "Total execution time: ".$elapsed);        
    }



Implementing some code - El Forum - 09-14-2007

[eluser]Michael Wales[/eluser]
I'll have to come back to this one a bit later on when I can look through Conteg's documentation. Sorry I couldn't help you out quicker - this issue is going to be a bit more involved than I initially thought.

Hopefully someone else can come through and point you in the right direction.


Implementing some code - El Forum - 09-14-2007

[eluser]manilodisan[/eluser]
Yep...I was hoping for someone from the team to look at this. I think I got it to stop the errors but fear that somewhere in the CI's engyne they get rewritten. My cache still expires in 1981 and that's not what I was hoping...I'm curious where do the headers get set.