Part of the EllisLab Network
This thread is a discussion for the wiki article: Cache headers
   
 
Cache headers
Posted: 21 July 2008 05:20 PM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  172
Joined  01-24-2008

Woah Woah Woah Waittaminute!

This is half of the code from Suffix cache, but it is the most dangerous half.

If the suffix is not used as well as the header, you will overwrite the same cache file with each different mime-type.  For example, if you are using your controller structure as the organizing principle behind your views and your assets (as I am), your page at example.com/welcome/index will be calling for a css at example.com/welcome/index.css and a js at example.com/welcome/index.js.  Without recognition of a particular suffix for each mime-type, the next time someone links to example.com/welcome/index they will get the cached javascript.

If you refuse to recognize suffixes, then you will require a separate controller for each mime-type.  I don’t want to see that happen to Codeigniter.

Use Cache headers as a library if you must, but a core change should be DRYer than that.

Profile
 
 
Posted: 08 June 2009 05:04 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  5
Joined  03-24-2009

My site uses xhtml with header “Content-Type: application/xhtml+xml; charset=utf-8” so IE doesn’t like it (I need different header for IE). Also I have to change some structure of my site especially for IE, so site content is a bit different for this browser. Because this lib is caching only copy file for all browsers this wasn’t enough for me. So I changed it a little:

In function _write_cache I’ve changed:

$uri $CI->config->item('base_url').
       
$CI->config->item('index_page').
       
$CI->uri->uri_string(); 

to

$uri $CI->config->item('base_url').
       
$CI->config->item('index_page').
       
$CI->uri->uri_string().
       (
stripos($_SERVER['HTTP_USER_AGENT']'msie')?'msie':'other'); 

And in function _display_cache:

$uri $CFG->item('base_url').
       
$CFG->item('index_page').
       
$URI->uri_string

to

$uri $CFG->item('base_url').
       
$CFG->item('index_page').
       
$URI->uri_string.
       (
stripos($_SERVER['HTTP_USER_AGENT']'msie')?'msie':'other'); 

Thanks to that mod I have two versions of cache files, one for IE and second for other browsers.
I hope this could be helpful for somebody smile

Profile
 
 
Posted: 17 June 2009 10:42 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  3
Joined  06-17-2009

Well I think I screwed up this post. This post is concerning the Cache headers. I added the code from the article and I am getting the following error. Not sure how to resolve this issue.

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/system/application/libraries/MY_Output.php:2)

Filename: libraries/Session.php

Line Number: 662

Any help would be appreciated

Profile
 
 
Posted: 18 June 2009 05:02 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  5
Joined  03-24-2009

Headers can only be send before any output commands. If somewhere in your script you print something headers can’t be send and you have such error.

Profile
 
 
Posted: 18 June 2009 03:08 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  3
Joined  06-17-2009

I’m at my day job and don’t have access to my code to experiment. To enable caching I need to call $this->output->cache(n); Now I have this in a controller method that is creating a css page. In the method I set the cache and the load the view with $this->load->view(‘name’);. The view then contains the code $this->output->set_header(); to set for the proper mime type for css, along with all the css code.

From what your telling me it appears that I would need to do set the caching after I set the header. Is this the case? I’ll experiment more with this this evening.

Thanks!

Profile
 
 
Posted: 21 June 2009 10:39 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  5
Joined  03-24-2009

I think that you have to set headers before calling view function, because view is printing output, so calling set_header from view is too late for setting headers. Try placing it like that:

$this->output->set_header('Content-type: text/css');
$this->load->view('css_file_view'$somedata); 
Profile
 
 
Posted: 22 October 2009 08:35 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  14
Joined  10-02-2008

New idea: Set the expiration time according to a possibly set ‘Expires’-HTTP-header.

Just add the following code after the line $expire = ...:

// Set default expiration time
        
$expire time() + ($this->cache_expiration 60);
        
        
// Look for expiration header
        
foreach($this->headers as $header)
        
{
            
if(substr($header[0]09) === 'Expires: ')
            
{
                
// Expiration header found, calculating the new $expire value
                
if($newexpire strtotime(substr($header[0]9))) {
                    $expire 
$newexpire;
                
}

                
// Break if this header can't be replaced
                
if( ! $header[1]break; }
            }
        } 

You still will have to activate the output caching yourself, but you could actually do this in the method set_header():

public function set_header($header$replace TRUE{
        parent
::set_header($header$replace);
        
        if(
substr($header09) === 'Expires: '{
            $this
->cache(1);
        
}
    } 

Ta daa! Instant autocaching…

Profile
 
 
   
 
 
‹‹ Another BBCode Helper      Secure Session ››