Page 1 of 1

Different cache settings per filetype

Posted: Mon Mar 10, 2008 5:37 am
by skoppel
Hello all,

I have a website that requires login and I loading the pages takes too long (see image below).
Now the page roughly consists of:
- The actual page: training.xml.php
- An external stylesheet: allStyles.css.php
- An external javascript: allScripts.js.php
- Some images
Page-loading.jpg
Page-loading.jpg (64 KiB) Viewed 349 times
The main pages of the program (in this case training.xml.php) change very frequently, so these may NOT be cached.
The stylesheet and javascript are generated dynamically but remain the same throughout the session, so these may be cached.
I have not found a way that lets the browser NOT cache the main page and DOES let it cache the external files (css/js/images).

I'm not sure if it's relevant, but the page uses an SSL connection and we use cookies to store the login sessions.

The code below is included in EACH document at the moment (so the main document, the css and the js).

Code: Select all

 
// Assure that SSL is being used
if ($_SERVER["SSL_SERVER_S_DN_CN"] == "secure.xxx.net") {
 
    //Session settings
    ini_set("session.use_only_cookies", 1);
    ini_set("session.save_path", "/home/xxx/sessions");
    session_cache_limiter("must-revalidate");
    session_start(); 
 
    //Compress the page
    ob_start("ob_gzhandler");
 
    // ACTUAL CODE HERE
 
}
 
Then the main document also has the following included:

Code: Select all

 
header("Cache-Control: max-age=0, must-revalidate");
header("Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
 
How can I enable caching for the JS and CSS while making sure that the main PHP file is NOT being cached?

Thanks!

Sander

Re: Different cache settings per filetype

Posted: Mon Mar 10, 2008 6:28 am
by Sekka
Try looking up Smarty. It is a template engine for PHP.

It comes with built in caching so it will fulfill your needs easily.

Re: Different cache settings per filetype

Posted: Mon Mar 10, 2008 3:54 pm
by skoppel
Thanks Sekka, looks good. But this is about server side caching, correct?
I need the stuff to be cached by the user...

Re: Different cache settings per filetype

Posted: Thu Mar 13, 2008 4:21 am
by skoppel
Basically the question is: is it possible to use headers in such a way that the CLIENT will cache everything EXCEPT for the main page.

Re: Different cache settings per filetype

Posted: Thu Mar 13, 2008 10:42 am
by Sekka
As far as I am aware browsers cache files depending on the last modified header of a file, assuming the client has caching on.

So all you need to do is add a header indicating when the file was last modified. I just randomly copy and pasted this off the PHP header doc page,

Code: Select all

header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
That takes the current date/time, you will cache the date/time and pass it in with each call.

Re: Different cache settings per filetype

Posted: Fri Mar 14, 2008 12:00 pm
by skoppel
Thanks Sekka. How does that differentiate between the different filetypes?