Different cache settings per filetype

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skoppel
Forum Newbie
Posts: 4
Joined: Mon Mar 10, 2008 5:06 am

Different cache settings per filetype

Post 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.01 KiB) Viewed 347 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
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Different cache settings per filetype

Post 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.
skoppel
Forum Newbie
Posts: 4
Joined: Mon Mar 10, 2008 5:06 am

Re: Different cache settings per filetype

Post by skoppel »

Thanks Sekka, looks good. But this is about server side caching, correct?
I need the stuff to be cached by the user...
skoppel
Forum Newbie
Posts: 4
Joined: Mon Mar 10, 2008 5:06 am

Re: Different cache settings per filetype

Post 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.
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Different cache settings per filetype

Post 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.
skoppel
Forum Newbie
Posts: 4
Joined: Mon Mar 10, 2008 5:06 am

Re: Different cache settings per filetype

Post by skoppel »

Thanks Sekka. How does that differentiate between the different filetypes?
Post Reply