Disabling SWF Caching in your BrowserDuring your Flash development you have probably noticed that when you test your SWF in the Browser that the browser never seems to update the Flash app to the one you just uploaded to your server. This is because the browser actually caches the SWF file and doesn't know that it has changed on the server. There is a way however to provide a PHP wrapper around the SWF to change this behavior. All you need to do is write a game.php that does the following:
<?php header('Content-type: application/x-shockwave-flash'); header('Expires: Thu, 01 Jan 1970 00:00:00 GMT, -1'); header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); echo file_get_contents('game.swf'); ?>This php will essentially act as a mediator between the browser request and the server. The browser still thinks this file is a SWF type request but the server tells the browser that additionally it should not cache the file. All you need to do now is just call the PHP file instead of the SWF file from your HTML page that displays the Flash app.
<object width="640" height="480" align="middle"> <param name="movie" value="game.php" /> <param name="bgcolor" value="#000000" /> <embed src="game.php" width="640" height="480" scale="noscale" bgcolor="#000000" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>Vuala, you now have a Flash app that reloads itself every request. You alternately may set the timeout/expire time to something like a week or whatever if you are updating your production Flash app every so often to force your users to update to the newest version on a somewhat consistent basis.
Now the same thing tends to happen inside of Flash when you do a URLRequest. You would think that something like the following would work to disable caching:
var loader:URLLoader = new URLLoader(); var request:URLRequest = new URLRequest(whatever url); request.requestHeaders.push(new URLRequestHeader("pragma", "no-cache")); request.requestHeaders.push(new URLRequestHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT, -1")); request.requestHeaders.push(new URLRequestHeader("Cache-Control", "no-cache, no-store, must-revalidate")); loader.load(request);WRONG! For some reason, Flash doesn't respect any caching rules; the only way to get around this is to trick Flash into thinking your url is a unique url every time.
var request:URLRequest = new URLRequest("whateverurl?nocache=" + new Date().getTime());This method has a drawback too though; if you are using URLVariables, there will be a conflict. The solution is to just make a URLVariable with our nocache trick:
var request:URLRequest = new URLRequest(whateverurl); var variables:URLVariables = new URLVariables(); variables.nocache = new Date().getTime(); // set up other URLVariables here request.data = variables;There you go, caching is now disabled!