obsidian Posted February 1, 2007 Share Posted February 1, 2007 Well, this is more an informative post, for those of you who care to read it I developed a banner rotator in flash for work that allows our content manager to simply update an XML file with the locations of banners and URLs for the banners to point to, and the presentation loads and rotates these banners, linking to the appropriate URL. Anyway, everything was working great until he started getting updated images, and when he would update his file, nothing would change on the display. After a little perusing, it was clear that it wasn't the content pages that were causing the issue, but rather the XML file itself was being cached. Simply doing a hard refresh (Ctrl + F5) of the content pages was not enough since this only forces a refresh of the local page, not those that are being embedded via HTML. Thankfully, I was aware of the no caching abilities of PHP, and I played around for a few minutes and was able to develop a solution that appears to work great. Here are the before and after codes (first was .xml, second is .php): images.xml <presentation> <item> <image>/images/banners/garage.jpg</image> <link>/giving/garage/</link> </item> <item> <image>/images/banners/camp07.jpg</image> <link>/campus/events/camps/</link> </item> <item> <image>/images/banners/cruise.jpg</image> <link>/balticcruise/</link> </item> </presentation> images.php <?php header("Content-type: text/xml"); header("Cache-control: no-cache, must-revalidate"); header("Expires: Mon, 1 Jan 2007 00:00:00 GMT"); ?> <presentation> <item> <image>/images/banners/garage.jpg</image> <link>/giving/garage/</link> </item> <item> <image>/images/banners/camp07.jpg</image> <link>/campus/events/camps/</link> </item> <item> <image>/images/banners/cruise.jpg</image> <link>/balticcruise/</link> </item> </presentation> With changing the content-type to text/xml, obviously, the browsers that support XML formatted code will recognize it as such. The second and third lines of PHP force the browser to reload the page every time the script is included from anywhere since the browser will recognize the code as expired. I know this isn't really a question, but it's something I've run into on a couple different occasions, and I thought it may help someone else out as well. Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted February 1, 2007 Share Posted February 1, 2007 Thats actually a good way to get around a problem ive been having... would never have though of using something as simple as that lol! Thanks -Andy Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 1, 2007 Author Share Posted February 1, 2007 Thats actually a good way to get around a problem ive been having... would never have though of using something as simple as that lol! Thanks -Andy Great! I wasn't going to post, but I figured it couldn't be that uncommon a problem: just not one I'd had to deal with before. Thankfully it's an easy fix, and hopefully I can save some others a little headache with the reminder of the caching controls in the HTTP header modifications Quote Link to comment Share on other sites More sharing options...
SharkBait Posted February 1, 2007 Share Posted February 1, 2007 Oh I think I might be able to use something like this. Some of my aJax pages don't update properly because IE reads from the cache all the time. I only have temporarily fixed it because I have manually set IE to always get a new version and never to read from the cache. I'll try the header thing, I know there was a way to do it but I guess I haven't really looked into it too much. Thanks! Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 1, 2007 Share Posted February 1, 2007 I have had this happen before when working with images and when I used the nocache headers it still didn't work, so what I did was add the timestamp to the end of the image url <img src="image.php?<?=time()?>" /> Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 1, 2007 Author Share Posted February 1, 2007 I have had this happen before when working with images and when I used the nocache headers it still didn't work, so what I did was add the timestamp to the end of the image url <img src="image.php?<?=time()?>" /> I've had to do that when helping to setup a webcam before. Very interesting how images don't seem to follow the same caching guidelines as other file types sometimes. Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted February 1, 2007 Share Posted February 1, 2007 Wow, this is weird. I started working on the exact same thing a few days ago (a banner for a website, rotating new products) and I ran into the same XML caching issue. I came up with the same non-caching headers, it was very annoying having to clear the cache until I added them. Anyway.. weirdly similiar project. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 1, 2007 Share Posted February 1, 2007 I've never run into this particular problem before, but knowing this could be helpful in the future. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.