HTTP/Browser Caching with .htaccess - php

I have a .htaccess in my root directory with the following setting.
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
</filesMatch>
I thought it caches any of the files in the filesmatch directive. One thing that I am not clear about is whether the browsers cache the actual HTML content that goes out (from index.php) or not. I don't want browsers to cache the HTML. I only want browsers to cache the images, css and js.
Thank you.

From what you are saying you are uncertain if the browser is caching your PHP file or what is generated from it, use these headers to be absolutely certain, you will never know unless you are explicitly setting rules for it:
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

Related

Server picking up old files

I'm still inexperienced as a web developer so I have exhausted my knowledge on my current problem.
I recently made changes to files on my one website and uploaded them via FTP to the server. When I go to the website it's still executing the old code.
I cleared all my cache and tried on different devices. Even after changing the file names for example "logout.php" to "logout_code.php" as well as link references to those names when I click on the logout button it still goes to "logout.php".
Can anybody shed some light?
Try setting a no-cache header on the page that redirects to the logout page.
header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
The above snippet goes at the top of the page before any other output is sent. (within a <?php ?> tag of course)

I hide HLS URL using PHP to output files but doesn't work

This is my original URL: http://xxx.xxx.xxx.xxx/01/MV01/index.m3u8
For security concern I moved all HLS files outside the document root of web site (/var/www/html).
IE. move them to /var/www/video/01/MV01/index.m3u8 including all .ts files in the same folder.
And then I created a .htaccess in the document root:
.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} .*ts$|.*m3u8$ [NC]
RewriteRule ^(.*) auth.php?file=$1 [NC,L]
This will redirect all requirement and get files outputted by php.
auth.php:
//
Some codes to check authorization first.
//
$reqpath = strip_tags($_GET['file']);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache,must-revalidate");
header("Cache-Control: post-check=0, pre-check=0",false);
header("Pragma: no-cache");
header('Content-type:application/force-download');
if (strpos($_GET['file'],".ts")>1) header("Content-type: video/MP2T");
if (strpos($_GET['file'],".m3u8")>1) header("Content-type: application/x-mpegURL");
#readfile("/var/www/video/".$reqpath);
My purpose is when user accesses http://xxx.xxx.xxx.xxx/01/MV01/index.m3u8 he still can play the video.
The result is:
It is working well on iOS and Android but can not play on PC including jwplayer and VLC.
The error message on jwplayer is " Error loading player: No playable sources found"
The error message on VLC is "main input error: no suitable demux module for 'http://xxx.xxx.xxx.xxx/01/MV01/index.m3u8'
**I use the HLS sample files download on apple.com so I think the .m3u8 and .ts files are no problem.
Help!
I have solved it finally.
Why can only play on iOS and Android but not on PC? I guess the problem should be in the outputting in PHP so I tried to change the HTTP header and then I found the answer.
Just add one more header:
header('Content-Length: ' . filesize("/var/www/video/".$reqpath));
All of the header here
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache,must-revalidate");
header("Cache-Control: post-check=0, pre-check=0",false);
header("Pragma: no-cache");
header('Content-type:application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Length: ' . filesize("/var/www/video/".$reqpath));
header('Content-Type: '.mime_content_type("/var/www/video/".$reqpath));
Hope this helps someone.

Downloading Files my download stops at 11MB using PHP

I am generating a zip file using zip archive and sending it to the browser for the user to download.
$archive_file_name = "/var/www/html/administrator/1396413991.zip";
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=test.zip");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Pragma: no-cache");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
readfile("$archive_file_name");
exit;
My download stops at 11Mb and i am unable to download files more than 11mb, What iam doing wrong or incorrect in this.
Any suggestion would be appreciated. Thanks.
I would advice in using fpassthru instead, it's been made specifically for this situation.
Note:
Passthru didn't work for me for files greater than about 5Mb. Just adding "ob_end_clean()", all works fine now, including > 50Mb files.
$ToProtectedFile=$pathUnder.$filename
$handle = #fopen($ToProtectedFile, "rb");
#header("Cache-Control: no-cache, must-revalidate");
#header("Pragma: no-cache"); //keeps ie happy
#header("Content-Disposition: attachment; filename= ".$NomFichier);
#header("Content-type: application/octet-stream");
#header("Content-Length: ".$SizeOfFile);
#header('Content-Transfer-Encoding: binary');
ob_end_clean();//required here or large files will not work
#fpassthru($handle);//works fine now
May be you can try and change your php.ini settings to
memory_limit = 128M
post_max_size = 300M

Cache Manifest Caching Itself

Really stumped on this one.
I'm trying to cache some resources with the HTML5 cache manifest (yes, I know, don't judge me), but the manifest seems to be caching itself.
The manifest file is actually a dynamic PHP script, so it has a .php extension.
The following headers are set (How do I completely disable caching in Cakephp?)
header('Cache-Control: no-store, private, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Expires: 0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Pragma: no-cache');
header("Content-type: text/cache-manifest");
The manifest contains the following in the NETWORK section:
NETWORK:
/cachemanifest
/cachemanifest/
/cachemanifest/*
*/cachemanifest/*
cachemanifest/
cachemanifest
/cachemanifest/manifest.php
cachemanifest/manifest.php
*
Of course, the manifest file itself is excluded from the CACHE: section.
There's also a .htaccess file in the same directory containing this:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/cache-manifest "access plus 0 seconds"
</IfModule>
The site is built with CakePHP, which apparently has some 'helpful' caching mechanisms that I don't know much about. I'd assume the PHP headers would bypass anything... PHP could do.
So, what am I missing?
Actually, it looks like one of these worked. It just takes 30+ seconds for the cache to realize that it needs to update.
The way cache manifests work is that for the manifest to update itself some text inside the manifest has to change, even if it's just one letter. This can be done by having a build automator add a timestamp every time there is a change in code.

Cannot overwrite Cache-Control in PHP

I've moved a older site to a new server, and the client has found a very odd behaviour.
Very close to the end, I have this code:
if (!$this->cache) {
header('Expires: '.gmdate('d M Y H:i:s', 946684800).' GMT');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
}
Now the odd thing is the Cache-Control line doesnt work.
After packet sniffing I see this:
Expires: 01 Jan 2000 00:00:00 GMT
Cache-Control: max-age=300, public
Pragma: no-cache
The order of the headers is exactly how I set them, but the Cache-Control is completely different. I've grepped my code for any mention of cache-control and there is only that mention, and another one designed to force caching in a different file but it is a different line to what I'm seeing so it cant be the culprit.
Does anyone know why Cache-Control is changing?
If you are using sessions, it's possible that PHP is overwriting them. Take a look at session_cache_limiter() in the manual.
Alternatively, you can try setting those headers after you call session_start().
(Edit: I missed the bit about "Very close to the end," so maybe this isn't your problem.)
I think you are running Squid, you should check its config for:
header_replace Cache-Control max-age=300, public

Categories