AskJimBob - Redneck Programming at it's Finest!
 Redneck Categories Preventing browsers from caching content via randomization
Home Page
Web Application Design
Web Dev Tips

With Coldfusion it's pretty easy to control the headers returned to the browser with the <.cfheader> tag, so it's pretty easy to instruct browsers not to cache your content, but what about content you include from other websites, like advertising providers? The browser receives a different set of headers from those sites, who may or may not want their content to be cached and may or may not know how to keep browsers from caching the content they deliver. There's an easy fix for this, by inserting a random number into the URL used to call the ad.

For example, let us say that your ad provider gives you the following code to insert into your page:

<a href="http://www.myadprovider.com/adClick?id=095235&pb=X7eA91JeWoDje231wRPl">
<img src="http://www.myadprovider.com/showAd?id=095235&pb=X7eA91JeWoDje231wRPl">
</a>

And you notice that even when you refresh your page the ad never changes (and it's supposed to), then simply use a random number, generated by your programming language of choice. Frankly, it can easily be done in javascript, but that's not what we teach here, so let's have a look at it in good ole Coldfusion:

<!--- generate a random number --->
<cfset myRand = RandRange(100000,99999999)>
<!--- insert the ad code inside a <cfoutput> tag --->
<cfoutput>
  <a href="http://www.myadprovider.com/adClick?id=095235&pb=X7eA91JeWoDje231wRPl">
    <!--- add myRand to URL --->
    <img src="http://www.myadprovider.com/showAd?id=095235&pb=X7eA91JeWoDje231wRPl&stopCache=#myRand#">
  </a>
</cfoutput>

What's changed? We added some bogus additional variable to pass on the URL. This works 99% of the time, 1% of the time the web application being called errors because it doesn't recognize the variable name. The variable name is, therefore, arbitrary. Next we give it the value of 'myRand', which is a number between 100,000 and 99,999,999 (or whatever you set RandRange to).

Now each time your page loads it will have a new random number inserted into the URL for the ad (or other content) which will trick the browser into thinking it's new content and request it (again) from the serving site. This works for pretty much any content you pull in - objects (video, audio, etc.) and anything else that get's cached from another site in the browser.

 



Copyright © 2006 - 2008 Virtual Solutions Group. All rights reserved.