0

I am using php for generating javascript dynamically based on different conditions. I want browser to be able to cache it for specific amount of time.

I am using the following code for caching, but it doesn't seem to be working.

header("Content-type: text/javascript; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);

Also, The js file is quite big, ~290 KBs, So I want to send it compressed to the browser to reduce download time. any suggestions?

1 Answer 1

3

You forgot the Expires header name, So the line

$ExpStr = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";

Should be

$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";

// output your js file here

And for compressing the output, uncomment ( or add if not exists) the following line in PHP.ini. to deflate or compress the output. Don't forget to restart the server after modifying PHP.ini

output_handler = ob_gzhandler
Sign up to request clarification or add additional context in comments.

2 Comments

And if he hasn't got access to the php.ini he could still use ob_start("ob_gzhandler"); to compress all output.
@louis-h Thanks for another way, though fortunately I have access to the php.ini file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.