Reputation: 1
I need to store the javascript and page details to browser cache using http headers.
Can anyone help me to get this?
Many thanks
Upvotes: 0
Views: 198
Reputation: 47966
I think there is some confusion about exactly what you are wanting to cache. Two items are being referred to here -
To cache the first item (the page), setting the headers with PHP should cache the HTML contents of the page.
header("Cache-Control: public");
header("Expires: Tue, 08 Oct 2013 00:00:00 GMT"); // Date in the future
This will cache the contents of the page but not necessarily the files referenced by it. For example, if you had in your HTML file this code -
<script src="http://domain/some/js/file.js" type="javascript" ></script>
Then that text will be caches but not file.js
. To manually set the cache on those external files, you'll need to serve them using PHP and manually set the headers.
You'll want to do something similar to this -
<script src="another_file.php" type="javascript" ></script>
Now in another_file.php
you are going to want to load the JavaScript file and "echo" it out with the appropriate headers -
$file = '/absolute/path/to/your_script.js';
if (file_exists($file)) {
header('Content-Type: text/javascript');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
Upvotes: 0
Reputation: 9444
You could use HTML meta:
<meta http-equiv="Cache-control" content="public">
or
PHP headers:
header("Cache-Control: public"); // HTTP/1.1
header("Expires: Tue, 08 Oct 2013 00:00:00 GMT"); // Date in the future
Source: [PHP Manual]
Upvotes: 2
Reputation: 47966
How about simply setting the expire date in a header -
header("Cache-Control: public");
header("Expires: Tue, 08 Oct 2013 00:00:00 GMT"); // Date in the future
It should be noted that modern browsers do a good job caching resources. Usually these methods are used to force reloading a resource; To prevent browser cache.
Upvotes: 0