Jump to content

Image still caching, or possibly problem with ajax & session?


Recommended Posts

I am having a problem where I suspect images are being cached, despite using the following headers from the PHP.net manual:

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

 

Here is the page, and the steps to create the problem:

http://www.cubedpop.com/create/tags/

It should load a rectangle "tag" with the current timestamp in it. The tag object is stored to the session.

Click on Tag Shape and a php page is loaded via Ajax (JS from to Mootools.net) with a bunch of shapes.

Click any one of them but the rectangle. This loads a new page via ajax. This page changes the shape of the tag, and saves it to the session. Then it "draws" the image:

print '<img src="http://cubedpop.com/create/process/draw/tag/" />';

 

IF I change this to below, it works perfectly. However, I hate the ugly URL it creates. There has got to be a way to stop the caching! Why do my cache headers not work?

print '<img src="http://cubedpop.com/create/process/draw/tag/?t='.time().'" />';

 

If it is NOT the headers/caching issue, here is more info:

Note: I use an htaccess file to redirect all pages to a base file which includes the library files and the session_start(), site-wide constants, etc.

Here is the  code of the draw tag file:

<?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

$drawTag = $_SESSION['drawTag'];
header("Content-type: image/png");

// Create the image
$im = imagecreatefrompng(TAG_SHAPE_IMG_URL.$drawTag->shape->image);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

imagettftext($im, 14, 0, 50, 50, $black, FONT_PATH.'tahoma.ttf', time());

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?> 

 

As you can see, the image is not updated when you change the shape. However, if you make the change, then view just the image in your browser: http://www.cubedpop.com/create/process/draw/tag/ The timestamp will update, and the new shape appears. Because the change is made I can tell the session was saved properly. So, this leads me to think the image is caching.

 

Also, if I print_r($_SESSION) on the page which changes the shape and then displays the image, the session contains the correct new shape in it, yet the image is wrong, and the timestamp is old.

 

Help...!

Cache issues are usually problems with specific browsers *cough*IE*cough*. What browser are you testing with?

 

Try using session_cache_limiter('nocache'); instead of those header lines.

 

 

The site is loading very slow. Some of the things are timing out for me.

I was testing in Fx.

Doesn't the session_cache_limiter affect the session? Not things like images?

 

I was trying to upload a very large file just now, that might be why it was slow. I am heading out so if it is still slow later I'd love to know - I'm using MT so I will yell at them if it's slow! :)

I had a similar issue recently. Calling an xmlhttp request on page load in IE after a db update always displayed the page without the latest updates. Firefox was fine.

 

The cure - it was the request that was being cached by IE (URL was same so it doesn't bother re-sending, just displays the last results) so adding current timestamp to the URL (making it different every time) resolved the issue.

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.