Optimising stylesheet.php

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
tamlyn

Optimising stylesheet.php

Post by tamlyn »

I've been trying to streamline the operation of stylesheet.php. I noticed that a PHP session is automatically started in include.php but (as far as I could see) not used during a request to stylesheet.php. The problem is that PHP sends lots of cache control headers to prevent caching of any content while a session is running which is clearly not helpful in this case.

My current solution is messy (define a var before including include.php and only start session if that var is not set) but I was wondering what the session is used for aside from admin logins and is it really necessary?

I'm also trying my luck with ETags and If-None-Match - I'll let you know how I get on.
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Optimising stylesheet.php

Post by Ted »

It's true that the session is only really used for admin related stuff.

What I would like to do if I had time is take the majority of include.php and split it up a bit.  Move a lot of it into the methods of the global object and have include.php just call them.  That way, stylesheet.php could not include include.php, and instead of initialize what it needs.
tamlyn

Re: Optimising stylesheet.php

Post by tamlyn »

That all sounds a bit advanced for me!

What I did with singapore was to to only start the session if it has already been started on a previous page or you are on an admin page. I implemented this in CMS by changing line 35 of include.php to

Code: Select all

if(!@session_id() && (isset($_REQUEST[session_name()]) || $CMS_ADMIN_PAGE)) {
It /seems/ to be working OK though I can't vouch for it. Basically if you delete the CMSSESSID cookie locally then CMS will not start the session until you visit an admin page (even if you don't log in). At this point the CMSSESSID cookie will be created and the session will be started on that and all subsequently viewed pages (including stylesheet.php). This seems like a good temporary solution since the vast majority of visitors will not visit an admin page.

Still working on the ETag stuff.
tamlyn

Re: Optimising stylesheet.php

Post by tamlyn »

OK I think I've got the ETag stuff working now (though only on Apache or servers that implement the PHP getallheaders() function).

Add the following code in stylesheet.php just after the content-type is sent.

Code: Select all

#Do cache-control stuff but only if we are running Apache
if(function_exists('getallheaders')) {
  $headers = getallheaders();
  $hash = md5($css);
  
  #if browser sent etag and it is the same then reply with 304
  if (isset($headers['If-None-Match']) && $headers['If-None-Match'] == '"'.$hash.'"') {
    header('HTTP/1.1 304 Not Modified');
    exit;
  } else {
    header('ETag: "'.$hash.'"');
    #header("Cache-Control: maxage=".(60*60*24));
  }
}

#sending content length allows HTTP/1.0 persistent connections
header("Content-Length: ".strlen($css));
If the CSS hasn't changed since the browser last loaded it then the script will return "304 Not Modified" and exit, saving time and bandwidth. Remember though that this will only work in conjunction with the modification to include.php mentioned above. Otherwise PHP will prevent any form of caching, making the ETag irrelevant.
tamlyn

Re: Optimising stylesheet.php

Post by tamlyn »

Hello, just bumping my thread because it seems to me this is a very worthwhile addition. It's been in use on sgal.org for over a month with no negative side-effects that I know of. I've just had a pore over the logs to see if it made much difference and the bandwith used by stylesheet.php has been reduced by 2/3 (as a proportion of total bytes served) but that isn't really much of an indication of anything. The number of 304 response codes served has also gone up but again this doesn't mean much.
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Optimising stylesheet.php

Post by Ted »

On a last minute bug fix round, I implemented this code.  Seems to work well for me.

Unless something major happens between now and tomorrow, it'll be in 0.11.

Thanks for reminding me!
tamlyn

Re: Optimising stylesheet.php

Post by tamlyn »

Cool :)
Post Reply

Return to “Developers Discussion”