Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[howto] php trick to make sure clients get the latest CSS version
The Problem
I am trying to solve the age old problem that occurs when a CSS file is updated but regular site viewers and clients don’t know it as they still have the old CSS file in their browser cache.
My solution
What i am trying to do is pass the file date as a parameter at the end of the URL to the CSS file so if the file in the cash is older then the newer version should be picked up.
<txp:php>
$style = "css/thestyles.css";
$get = '?date='.date('Y-m-d',filemtime($style));
print('<link rel="stylesheet" href="'.$style.$get.'" type="text/css" media="all" />');
print("\n");
</txp:php>
What does this error mean?
Tag foutmelding <txp:php> -> Warning: filemtime() [function.filemtime]: stat failed for css/thestyles.css on line 3
This works fine outside of textpattern in a standalone .php file but not in textpattern. Any ideas?
EDIT: The code is within a txp form and not op the template page.
Last edited by Timid&friendly (2009-06-04 15:48:06)
I think, therefore I AM, … … er … I think :-?
Offline
Re: [howto] php trick to make sure clients get the latest CSS version
Timid&friendly wrote:
Warning: filemtime() [function.filemtime]: stat failed for css/thestyles.css on line 3@
The CSS files in PHP are not actual files (unless you’re using the rvm_css plugin); they are served via trickery from the database. Look at the HTML source code of your pages and you’ll see the name of the css file is appended to a standard css.php link as a “?” variable.
Thus, PHP has no ‘file’ to read the filemtime. Think you’ll have to install rvm_css or find a different approach I’m afraid.
EDIT: the txp_css table also has no concept of ‘last modified’ either :-( but you could perhaps do it yourself by hashing the contents of the ‘css’ column via md5()
, storing it somewhere and then compare that to the current hash each time. If they differ, add a random string like '?force='.mt_rand()
to the end of the css file to force a refresh. There’s probably a better way though.
Last edited by Bloke (2009-06-04 15:56:34)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: [howto] php trick to make sure clients get the latest CSS version
I’m not using the inbuilt txp CSS. I have real external files and normally access them in this way:-
<link href="<txp:site_url />css/general1.82.css?v1" rel="stylesheet" type="text/css" media="screen" />
I guess what you are saying is the error indicates that the script can’t find the file, is that correct?
EDIT: even when i use the correct absolute path i still get this error?
Last edited by Timid&friendly (2009-06-04 16:01:33)
I think, therefore I AM, … … er … I think :-?
Offline
Re: [howto] php trick to make sure clients get the latest CSS version
Timid&friendly wrote:
I have real external files… I guess what you are saying is the error indicates that the script can’t find the file, is that correct?
Ah, my bad. Yes. It’s just PHP’s way of saying “file not found” or “I don’t have enough permissions to find out”.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: [howto] php trick to make sure clients get the latest CSS version
Wierd? CH MOD is set to 777 and typing the url in browser brings the file up?
I think, therefore I AM, … … er … I think :-?
Offline
Re: [howto] php trick to make sure clients get the latest CSS version
Timid&friendly wrote:
Wierd? CH MOD is set to 777 and typing the url in browser brings the file up?
*shrug* a stab in the dark then: also remember that you need to specify the path to the file (/home/my/hoster/textpattern/css/thestyles.css
) and not the URL to the file in order to have filemtime()
work. The exception is if you are running with PHP5+ and have the relevant permissions to use a URL wrapper as a file. I think… I don’t have the luxury of being able to find out :-(
Last edited by Bloke (2009-06-04 16:18:49)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: [howto] php trick to make sure clients get the latest CSS version
You da man!
That was it. Thx so much.
I think, therefore I AM, … … er … I think :-?
Offline