Forum Moderators: coopster

Message Too Old, No Replies

PHP and Caching

         

pmmenneg

8:53 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



Hi all. I am using PHP and JPGRAPH to generate a dynamic image, a chart that is based on user ratings.

As I recently added some ajax functionality to my page, this image generation is now an issue. Essentially, a user would rate an object, the page would refresh as the rating was submitted and when the user was taken back to the page, the new image would load with the new data they had just submitted. As this rating system now uses ajax, there is no page refresh happening anymore...

The image is of the format

<img src="/graph/ratings.php?x=300&y=150" />

I use js to write out a new image tag with the new data upon success of the ajax call, and if I load the image in a new window, it is properly updated, but on the page the user is using and has already seen the image prior to updating the underlying data, the browser is seeing the image url as being the same and not 're-loading' the image at all.

A solution I've tried is adding some random txt to the url, so that the browser thinks it's a new image. This works, but seems like a hack.

So, is there a way in the called php script I could set the headers or something on this particular image so that it is never cached, not even for the user session?

Thanks,

P

pmmenneg

9:02 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



BTW, I have tried adding

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Thu, 1 Jan 2009 00:00:00 GMT');

To the script (/graph/ratings.php) that generates the image, but that doesn't seem to have any effect.

Thanks

inveni0

9:04 pm on Mar 18, 2009 (gmt 0)

10+ Year Member




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

Use it in conjunction with random text on the url, because the PHP method doesn't always work.