PDA

View Full Version : Dynamic Ajax Content - caching problem



rleeden
02-07-2006, 04:07 PM
Dynamic Ajax Content script
http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

I've recently started using the above script to create dynamic content on one of my websites. It works very well in Firefox, but I'm having problems in IE and Opera.

Basically the website is a random fortune cookie generator - a very stripped down test version can be seen at http://fortunecookie.rleeden.no-ip.com/ajax.php.

In Firefox everytime you click the Fortune link a new fortune cookie is displayed - the php script fortune.php generates a new fortune. But this doesn't happen in IE or Opera as the output from the fortune.php script is getting cached. I can see from my apache logs - the fortune.php script is only called once. I can also see this when clearing my IE cache, a new fortune is displayed.

Anybody know how to prevent this caching problem? Any help would be appreciated.

rleeden
02-07-2006, 05:28 PM
Further to the above, I've tried the same website from my home PC and the results are different than from my office PC. Firefox and IE both work fine, but Opera still doesn't work.

I'm not sure what the difference is. I'm not using a proxy at work so that can't be it. Anybody any ideas? Anybody know how I can ensure that the output from fortune.php isn't cached?

rleeden
02-08-2006, 07:17 AM
Sorry, to be adding a third post to a thread that I started, but I've fixed the problem. Using PHP headers seems to have solved the problem.

i.e. putting the following lines at the very top of my fortune.php script:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

ddadmin
02-08-2006, 11:16 AM
Thanks for the tip. Another way to bust cache is to modify the line:


page_request.open('GET', url, true)

inside the ajax script to something like:


page_request.open('GET', url+"?bustcache="+new Date().getTime(), true)

This adds a constantly changing number to the query, busting cache in IE.