Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

header expires on PHP file with JS output

How to put an expires header on a PHP file which outout a JS file?

.htaccess

ExpiresActive on
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType application/javascript A29030400

header on JS file

Cache-Control   max-age=29030400
Connection  Keep-Alive
Date    Thu, 18 Oct 2012 09:23:16 GMT
Etag    "300000002c8ba-15f-4cc3069c72d00"
Expires Thu, 19 Sep 2013 09:23:16 GMT
Keep-Alive  timeout=5, max=94
Server  Apache/2.2.22 (Win32) PHP/5.4.3

header on PHP file which outputs a JS file

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Length  1195
Content-Type    application/javascript
Date    Thu, 18 Oct 2012 09:23:16 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=5, max=100
Pragma  no-cache
Server  Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By    PHP/5.4.3

code PHP file

<?php
header('Content-Type: application/javascript');
$js_output = array('something' => 'some value');
?>
var <?=json_encode($js_output)?>
like image 295
clarkk Avatar asked Dec 07 '25 02:12

clarkk


1 Answers

You can set the Cache headers:

<?php
header('Content-Type: application/javascript');
header("Cache-Control: max-age=29030400");
header("Cache-Control: public", false); 
$js_output = array('something' => 'some value');
?>
var <?=json_encode($js_output)?>

see also: http://php.net/manual/en/function.header.php

like image 91
Niborb Avatar answered Dec 08 '25 16:12

Niborb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!