Forum Moderators: coopster

Message Too Old, No Replies

Caching JS generated through php

         

asantos

8:11 pm on Nov 15, 2006 (gmt 0)

10+ Year Member



Hi. Im developing a service where my registered users must include a javascript on their websites.

Since each user has different config settings on my service, they must all identify themselves with an ID, for example, user John Doe must include this piece of code in his site:

<script src="http://blabla.com/script.php?id=384" type="text/javascript"></script>

That way the javascript that will get printed will use his own settings. What i need to do now, is to reduce my server's requests by properly caching the data. A friend told me to use this in the top of script.php:

header("Cache-Control: must-revalidate");
$offset = 60*60*24*7;
$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s",time() + $offset)." GMT";
header($ExpStr);
header('Content-Type: application/x-javascript');
...

That way if a user comes back to John Doe's website, my server wont have to re-generate the whole code, and they will use the one in their cache.

Is that enough? Will it work? Thanks.

asantos

2:47 pm on Nov 17, 2006 (gmt 0)

10+ Year Member



anyone?

Vastio

3:04 pm on Nov 17, 2006 (gmt 0)

10+ Year Member



These are the headers that I normally use for preventing caching. They seem to work well.

<?php
header("Expires: Mon, 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");
?>

asantos

5:33 pm on Nov 18, 2006 (gmt 0)

10+ Year Member



but i NEED the php generated JS to be CACHED.

asantos

6:17 am on Nov 24, 2006 (gmt 0)

10+ Year Member



anyone?

FiRe

6:20 pm on Nov 24, 2006 (gmt 0)

10+ Year Member



Try this, found it after searching a bit...
<?php
Header("Cache-Control: must-revalidate");

$offset = 60 * 60 * 24 * 3;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
Header($ExpStr);
?>