Jump to content

Interesting caching issue - PHP to the rescue!


obsidian

Recommended Posts

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.