Refresh: make the browser use the files in cache [duplicate] - php

How can I setup expires headers in PHP + Apache? I'm currently using an auto_prepend to serve resources gzipped but I'd also like to maximise the HTTP cache.
How can I set these up?

There are two ways to do this. The first is to specify the header in your php code. This is great if you want to programatically adjust the expiry time. For example a wiki could set a longer expires time for a page which is not edited very often.
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour
Your second choice is to create an .htaccess file or modify your httpd config. In a shared hosting environment, modifying your .htaccess file is quite common. In order to do this, you need to know if your server supports mod_expires, mod_headers or both. The easiest way is simply trial and error, but some Apache servers are configured to let you view this information via the /server-info page. If your server has both mod_expires and mod_headers, and you want to set the expiry on static resources, try putting this in your .htaccess file:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
For other combinations and more examples see: http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html

This Apache module might be of help:
http://httpd.apache.org/docs/2.0/mod/mod_expires.html

Did you try something like?
<?php
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
?>

Related

I don't get the website's up-to-date html

I changed the .htaccess file to cache everything for a year.
After a while, I noticed that I don't see the updates I make to the website.
I figured out, that because I changed the default cache to a year, somehow the html file got cached for 1 year, and even though I've changed it back, still doesn't work.
The only way I could make it update is to use different links (like www.domain.com/newlink insead of www.domain.com/oldlink), but that doesn't repair my frontpage, as it is www.domain.com, and I can't change that.
Do you have any solution for my problem?
P.S. It isn't my browsers cache, as I have cleared it's cache, I even reinstalled it, and if that's not enough, I can't see the updated version on my phone.
P.P.S. I can see that the http request doesn't even reach the server for the www.domain.com link.
P.P.P.S. I tried using a proxy, and the page worked as it should, so I think it's only cached at my ISP.
Can you try to explicit say not to cache to apache in the htaccess. Also restarting your apache server might work.
<filesMatch "\.(html|htm|js|css)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>

caching using .htaccess - one image not caching weirdness

I'm on a shared hosted linux server so I must use .htaccess. I'm busy working on compressing and caching things. I actually have two questions, but first here is how I have the cache setup in my .htaccess file.
ExpiresActive on
<FilesMatch "\.(bmp|png|ico|gff|jpg|jpeg|gif|gcf)$">
FileETag MTime Size
ExpiresDefault "now plus 60 minutes"
</FilesMatch>
Question 1, so this does cache those things with the exception of one PNG file.
Now most of my files are all lowercase as I'm on Linux but a few PNG files have slipped through with and upper case extension.
What's strange is that all of the PNG files cache except for one called addon2.PNG. At first I thought it was because of the case but I have checked and I have 3 other PNG files with uppercase extensions - which Google Page Speed says are cached. So any ideas or is Google Page Speed just B.S.?
And question 2, as I'm wary due to my hosts mess up with their Varnish issue I'm adding things to cache a little at a time and waiting to see if my stuff screws up. When I try to cache HTML files the login/logout features of my site—written in PHP—do not work.
You have to login and refresh or logout and refresh. I'm wondering is that because the page HTML is output via the PHP file? All my main pages are PHP and I only have a few actual html files. But I thought caching HTML would just do those files with htm & html extensions using the code below. But it's kinda like the server is trying to cache the html outputted by the PHP files. Am I out of my mind here?
ExpiresActive on
ExpiresDefault "now plus 60 minutes"
ExpiresByType text/html "now plus 60 minutes"
<FilesMatch "\.(css|bmp|png|ico|htm|gff|html|jpg|jpeg|gif|gcf)$">
FileETag MTime Size
ExpiresDefault "now plus 60 minutes"
</FilesMatch>
Your long explanation basically boils down to a PNG image is not being cached & HTML is not refreshing. It could be that the PNG image you say is having issues (addon2.PNG) is simply damaged. But the problem with your question is you have not provided any headers to show what the output is.
If you are using Linux, just go to the terminal and use curl to get headers. For example, this is how I can get headers sent with the main Google image on their homepage:
curl -I https://www.google.com/images/srpr/logo11w.png
And the output of that command is:
HTTP/1.1 200 OK
Content-Type: image/png
Last-Modified: Wed, 09 Oct 2013 01:35:39 GMT
Date: Tue, 17 Dec 2013 23:39:21 GMT
Expires: Wed, 17 Dec 2014 23:39:21 GMT
X-Content-Type-Options: nosniff
Server: sffe
Content-Length: 14022
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=31536000
Age: 1647616
Alternate-Protocol: 443:quic
So in your case just type in this command—remember to set it to the real path of the image—like so:
curl -I https://url/to/your/site/addon2.PNG
And compare the headers against something you know is caching as you wish it to.
Also, ExpiresDefault & Cache-Control can sometimes slightly conflict with each other. So check those two values to see what your host is setting versus what your .htaccess is setting.
And regarding this:
So any ideas or is Google Page Speed just B.S.?
All of those page benchmarking tools have varying usefulness. In general for the specific issue you are facing, they are not B.S. but utterly useless. It’s like you are in a car with an oil leak & some automatic system in the car is telling you to change your oil. You don’t need to “change your oil” you need to find the leak & plug it up so the oil stays in!
So in your case the best bet is to using Unix tools like curl to check what the header output is & adjust/tweak until you get it right.
EDIT Also, please look over this pretty in-depth—and useful—caching guide from Google.

php caching and .htaccess caching

There is another thread similar to this that was closed and that didn't have any useful information in it: https://stackoverflow.com/questions/11955822/php-file-caching-vs-cache-through-htaccess
Is it necessary to implement a php caching system if you are caching through .htaccess? Here is my current .htaccess caching:
<IfModule mod_headers.c>
# Cache Media Files
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Mon, 20 Apr 2015 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
# Cache JavaScript & CSS
<FilesMatch "\.(js|css)$">
Header set Cache-Control "public"
Header set Expires "Mon, 20 Apr 2015 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
# Disable Caching for Scripts and Other Dynamic Files
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
</IfModule>
with this file caching, will building out a php caching system improve my site even more? Or would it make more sense to compress data in .htaccess and use php to cache? I'm just trying to understand which method of caching will improve a site more or if using both is recommended.
For static files, you can cache them by HTML Headers tags, and .htaccess
The browsers will cache them in local machines.
For Dynamic content with .PHP, you can cache widget, objects to reduce the query call to mysql database.
You can try this one. Example, it cache $products in 600 seconds, and your PHP only send 1 request to database. If you have like 500 visitors online, your page still use 1 query from first visitors to serve 500.
<?php
include("php_fast_cache.php");
// try to get from Cache first.
$products = phpFastCache::get("products_page");
if($products == null) {
$products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
// set products in to cache in 600 seconds = 5 minutes
phpFastCache::set("products_page",$products,600);
}
foreach($products as $product) {
// Output Your Contents HERE
}
?>
If you are using Wordpress, you can see all the cache plugins, they cache your content by PHP ( Files or Memcache ), and cache your images, css, js by .htaccess
We need both of them together will speed up the site and save bandwidth / CPU
You're doing client-side caching for static files only.
Caching in PHP solves a completely different problem - server-side performace issues of your application. So you should use it if your site is loading too slowly or if you're causing high server load.
There are many strategies how to implement server-side caching and it's up to you what fits best your application.
For example you can cache SQL queries results or you can cache HTML output of whole webpages. Do not forget about cache invalidation when your data changes.

FLV files caching. Set header

I have flv files on my server which have changing content but not necessarily changing names.
I am having an issue whereby the flv files send headers which set the file to cache. As sometimes the same user may require the same file with different content later the files need to tell the browser not to cache them.
I have tried using something similar to PHP's header() command but when I run:
Curl -I myfile.com/file1.flv
The headers are still there.
Any help please?
I am not sure how you tried using PHP for this, it is apache which is processing and dispatching the file so best to start there.
Try the below:
1) Enable headers.load (headers module) on apache. Else wont work.
2) add below to .htaccess. This will capture file types of all the below formats and set them not to cache.
<FilesMatch "\.(jpg|gif|js|css|ico|swf|zip|pdf|doc|htc|xls|rtf|odt|wav|mp3|avi|wmv|mov|txt|flv)$"> FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</IfModule>
</FilesMatch>
3) restart apache.
4) Try 'curl -i www.url.com/file.flv' command again.
You should see the headers tell the file to not cache.

Setup HTTP expires headers using PHP and Apache

How can I setup expires headers in PHP + Apache? I'm currently using an auto_prepend to serve resources gzipped but I'd also like to maximise the HTTP cache.
How can I set these up?
There are two ways to do this. The first is to specify the header in your php code. This is great if you want to programatically adjust the expiry time. For example a wiki could set a longer expires time for a page which is not edited very often.
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour
Your second choice is to create an .htaccess file or modify your httpd config. In a shared hosting environment, modifying your .htaccess file is quite common. In order to do this, you need to know if your server supports mod_expires, mod_headers or both. The easiest way is simply trial and error, but some Apache servers are configured to let you view this information via the /server-info page. If your server has both mod_expires and mod_headers, and you want to set the expiry on static resources, try putting this in your .htaccess file:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
For other combinations and more examples see: http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html
This Apache module might be of help:
http://httpd.apache.org/docs/2.0/mod/mod_expires.html
Did you try something like?
<?php
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
?>

Categories