I have a php file that is renamed in htaccess as a css file. The reason being, is because I have some styles and colors which change based on some admin options.
Anyway, I am trying to allow a visitor's browser to cache the file. Here's what I have:
style.php:
header("Content-type: text/css; charset: UTF-8");
// Start normal CSS styles...
.htaccess:
RewriteRule ^assets/css/min/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)\.css$ assets/css/min.php?style=$1&layout=$2&ver=$3 [L,QSA]
# Compress
AddOutputFilterByType DEFLATE text/css
# Cache for 1 week
<FilesMatch ".(css)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/css "access plus 1 week"
</IfModule>
In the page head:
<link rel="stylesheet" href="http://example.com/assets/css/min/blue/flat/0.9.2.css">
Each time a new page is loaded, the page contents are requested. These are the headers I receive:
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 13881
Content-Type: text/css; charset: UTF-8;charset=UTF-8
Date: Sat, 20 Feb 2016 22:49:17 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=94
Pragma: no-cache
Server: Apache/2.4.16 (Unix) OpenSSL/1.0.1p PHP/5.6.12 mod_perl/2.0.8-dev Perl/v5.16.3
Vary: Accept-Encoding,User-Agent
X-Powered-By: PHP/5.6.12
Your mod_expires config applies to files based on their mimetype as perceived by the webserver. Unless you've done some major surgery on the webserver config elsewhere, the webserver will not associate the text/CSS mimetype with files ending in .php (and if it did, much further hacking would be required to get them processed by the PHP parser). The header set by your PHP script is irrelevant to this process.
While it is possible to force mod_expires to add caching headers to the response, you'll also have to use mod_headers to remove the values PHP sets; when a browsers receives multiple conflicting caching instructions, it will resort to the shortest expiry time expressed in them.
Hence, to make the content cacheable, you should emit the caching information directly from the PHP script. E.g.
header('Cache-control: max-age=604800; private');
But basing the expiry time on the access time is not the best solution.
Related
I'm all of a sudden having a hard time downloading excel spreadsheets from our webserver.
We use apache2.2.22 and php 5.4.45-0+deb7u7.
The files are corrupt after download.
I've verified the spreadsheet files are OK. (I'm able to open them w/o problem if I don't download them via the browser then try to look at them.)
Here's the response header:
Cache-Control no-store, no-cache, must-reval…te, post-check=0, pre-check=0
Connection Keep-Alive
Content-Encoding gzip
Content-Length 22
Content-Type text/html
Date Mon, 29 Jan 2018 17:56:52 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive timeout=3, max=100
Pragma no-cache
Server Apache/2.2.22 (Debian)
Vary Accept-Encoding
X-Powered-By PHP/5.4.45-0+deb7u7
I'm just totally stumped here.
I've also been trying to add a mime-type like this:
case "xlsx":
header("Content-type: application/vnd.ms-excel-xml");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
Nothing seems to be working. (but it used to work flawlessly.)
Any help or ideas come much appreciated. Thank you.
I have an application where a number of otherwise static javascript files are being generated via PHP to allow configuration options to alter the static files (path like: mystaticfile.js.php). Everything works fine EXCEPT that I can't seem to get the cache settings to work and these resources are being reloaded with every page load.
The PHP file uses the following headers to try to set the cache settings:
$expires= 60 * 60 * 24 * 60; //cache for 60 days
header('Pragma: public');
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
header("content-type: application/x-javascript");
However, when the files are served they're showing headers that look like:
HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 19:18:00 GMT
Server: Apache/2.2.15 (CentOS)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 744
Keep-Alive: timeout=15, max=95
Connection: Keep-Alive
Content-Type: application/x-javascript
My first thought was that it was that it might be because Apache has the ExpiresActive flag set on but I don't see any ExpiresByType rules set for PHP files.
Reading online it sounds like ETag issues could be the problem, but I've added
Header unset Pragma
FileETag None
Header unset ETag
to the http.conf file (and restarted the service) and still no dice.
Any thoughts?
Source: PHP: Worry about some magical added “Cache-Control” Header ?
These headers are automatically set by the PHP Session module to
prevent browser/proxy based caching of your pages. Depending on your
environment setup, it’s possible to control these headers by using the
session_cache_limiter() function or use the php.ini
To disable these behaviour just pass an empty string to the
session_cache_limiter() function as mentioned in the documentation:
session_cache_limiter('');
I have the following code which I am using for my website. I would like to know if this code is correct in order for me to effectively cache me pages and files. I have tried to use tools to check this but some say they don't see that I am caching.
<ifModule mod_headers.c>
# 1 Month
<filesMatch ".(ico|gif|jpg|jpeg|png|pdf)$">
Header set Cache-Control "max-age=2419200"
</FilesMatch>
# 1 Week
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 1 Day
<filesMatch ".(htm|html)$">
Header set Cache-Control "max-age=86400"
</FilesMatch>
</ifModule>
Catching is the automatic mechanism of the browsers. When a page is loading, browser checks the cache for static files like js, css, images..., if they are not available browser will pus them into cache.
To determine whether the file is cached or not, check the firebug console and clik on request link.
here you can see information like
Accept-Ranges bytes
Cache-Control max-age=290304000, public
Content-Encoding gzip
Content-Length 2824
Content-Type application/javascript
Date Thu, 11 Jul 2013 10:15:06 GMT
Expires Fri, 12 Jul 2013 10:15:06 GMT
Last-Modified Thu, 03 Jan 2013 16:05:54 GMT
Server Apache
Vary Accept-Encoding,User-Agent
I am having problem serving downloads from our website. Large files just won't download in full. Download will stop somewhere in between... Example this file (size cca 172MB) won't download in full size (there are other files also).
I switched from entirely PHP-base download script, the one included in Kohana framework:
return download::force($filePath);
to a mod-xsendfile solution. I was reading about the possible problems with PHP based download scripts and large file and cam over mod-xsendfile is the right solution... Well looks like not, I am getting the same result with both techniques. My current download implementation using mod-xsendfile headers like this:
header("X-Sendfile: $filePath");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
What am I doing wrong?
UPDATE:
I used this HTTP sniffer to check response headers and this is the result if it helps solving this problem.
Status: HTTP/1.1 200 OK
Server: Apache
Set-Cookie: dewesoftsession=63ms5j67kc231pr4bpm8cmg1f7; expires=Sat, 30-Mar-2013 11:36:59 GMT; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: dewesoftsession=63ms5j67kc231pr4bpm8cmg1f7; expires=Sat, 30-Mar-2013 11:36:59 GMT; path=/
Content-Disposition: attachment; filename="DEWESoft_FULL_7_0_5.exe"
Last-Modified: Mon, 24 Sep 2012 12:50:12 GMT
ETag: "25814de-ac291e9-4ca7207c7fcd9"
Content-Type: application/octet-stream
Content-Length: 180523497
Date: Sat, 30 Mar 2013 09:37:01 GMT
X-Varnish: 294312007
Age: 2
Via: 1.1 varnish
Connection: close
X-Varnish-Cache: MISS
After couple of days we managed to find what cause the problem. Varnish has a start-up parameter called send_timeout which is set to 600s by default. With large file downloads you might run into this timeout which will cause your download to be interrupted.
So increasing Varnish's send_timeout parameter will help you solve this kind of issue.
I have very specific problem running PHP as Apache 2.2 module. When running PHP as CGI, everything works OK, but slow. The output I get in this case is:
Good charset
But when runing PHP as Apache module on the same environment, I get the following output:
Wrong charset
The only difference is that I add following lines to appropriate VirtualHost in httpd.conf
SetEnv PHPRC "C:/PHP5/"
ScriptAlias /local-bin/ "C:/PHP5/"
AddHandler application/x-httpd-php5 php
Action application/x-httpd-php5 "/local-bin/php-cgi.exe"
<Directory "C:/PHP5/">
Order allow,deny
Allow from all
<Files "php-cgi.exe">
Allow from all
</Files>
</Directory>
The website uses 2 databases - one MySQL and one MSSQL. The text which is displayed incorrectly is retrieved from MSSQL database. There aren't any problems with text which is retrieved from MySQL database.
Any ideas how to run PHP as Apache module and get the same output as running PHP as CGI?
My setup is: Apache 2.2; PHP 5.2.17; mssql PHP extension 7.0; mbstring PHP extension 4.4.4
Headers in CGI mode, returned by curl -i
HTTP/1.1 200 OK
Date: Mon, 08 Aug 2011 11:56:15 GMT
Server: Apache/2.2.16 (Win32) mod_ssl/2.2.16 OpenSSL/0.9.8o
X-Powered-By: PHP/5.2.13
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=3h4kpps0l02pecfgktonq7rjd6; path=/
Transfer-Encoding: chunked
Content-Type: text/html
Headers in PHP as a module mode:
HTTP/1.1 200 OK
Date: Mon, 08 Aug 2011 12:04:16 GMT
Server: Apache/2.2.16 (Win32) mod_ssl/2.2.16 OpenSSL/0.9.8o PHP/5.2.13
X-Powered-By: PHP/5.2.13
Set-Cookie: PHPSESSID=hi9h3skbsjvpcr7usdlf36d2t7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: text/html
I don't know what was the exact problem, but everything seems to work OK now. I just downloaded fresh PHP package and configured it from scratch. What I didn't mention is that I use ZendServer Community Edition and it seems to cause the problems (by the way with same PHP 5.2.17 version). When I configure virtual host to use my fresh PHP installation - everything works OK, but when I use ZendServer built in PHP it causes problems.
Any ideas how to run PHP as Apache module and get the same output as running PHP as CGI?
Normally the output does not differ because of the module type but because of configuration. Just keep the same configuration across SAPI types and you're done.
The website uses 2 databases - one MySQL and one MSSQL. The text which is displayed incorrectly is retrieved from MSSQL database.
Looks like retrieving from MSSQL fails. Check if the data is properly encoded in the database. Check the client encoding settings. This should solve your issue.
You should indicate the encoding with the content type, e.g.
Content-Type: text/html; charset=utf-8
This fixes most issues.