0

So I have desperately been looking for a way to set my website cached expiration. I would like to set the css and js expiration but I couldn't find any useful information. I tried to use

<meta http-equiv="Pragma" content="no-cache" />

but it just doesn't work. Most of the material online just talk about Apache, xml and others but I don't use them. Is there a way to implement the expiration using php or html? and how is it done?

7
  • Did you take a look to this post ? Commented May 24, 2016 at 8:50
  • @dtlvd yes but it's not html nor php. It says htaccess and I did try but it still doesn't work. Commented May 24, 2016 at 8:54
  • Are you using Apache to host your web app ? Commented May 24, 2016 at 8:59
  • Try as hard as you like, at the end of the day it is up to the users browser to choose what to cache. However, you can always pass a header() in PHP which is the same as what Apache does. Commented May 24, 2016 at 9:24
  • I do agree @Tigger but by default you can configure that with Apache and if the browser doesn't "override" that the Apache configuration should be effective. Commented May 24, 2016 at 9:41

1 Answer 1

1

Many different ways to do this. The following is a simple example only.

CSS file called test.css (do the same for the Javascript).

body { background:#abc }

A PHP file to render the requested CSS or Javascript called t.php that includes the no cache request headers:

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// use a switch statement for basic checking and flexibility  
switch($_SERVER['PATH_INFO']) {
    case '/css':
        echo file_get_contents('./test.css');
        exit;
    case '/js':
        echo file_get_contents('./test.js');
        exit;
}
?>

And the header section of the HTML/PHP file

<head>                              
<link rel="stylesheet" href="t.php/css" />
<script src="t.php/js"></script>
</head>                                         
Sign up to request clarification or add additional context in comments.

2 Comments

Great, best one so far. But I am having a problem with the css. It doesn't link the css. It works fine with the javascript.
@DanielOMensah It is most likely a file path issue. You can test by visiting the URI directly. If you let me know the path and file name of the CSS and HTML / PHP files, I'll update the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.