Stop caching my script!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Stop caching my script!!

Post by seodevhead »

I have a form page that users upload a picture of themselves and when the pic is uploaded and inserted into the DB, the page reloads and displays the new picture they just uploaded. However, the browsers keep caching my script and eventhough the new picture is uploaded and added to the database, sometimes it reloads and shows the old picture. I have the meta tags in the html for no-caching.. still caching though.

So I am trying to add these headers before the redirect. Would this work? Do these no-cache headers go before or after the 'header(location: ...)'??? Thanks for your help.

Code: Select all

# Redirect to reload page:
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: post-check=0, pre-check=0', FALSE);
	header('Pragma: no-cache'); 
	header("Location: http://www.mysite.com/add-picture.php");
	exit();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They will have no affect on the destination page. You could add a random, unique query string to the image URL. The image URL could be a script with the no-cache headers. The browser could still choose to cache the image data, but it would be misbehaving then.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Well... then it sounds like the easiest thing to do is make sure that every "new" uploaded picture has a new filename that doesn't match the old one... correct??? And as far as these headers:

Code: Select all

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', FALSE);
        header('Pragma: no-cache');
..it wouldn't do any good to just tag these right below my ob_start() in my header file, right?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Adding all those headers won't help. All you need to do is the following with the image element.

Code: Select all

<img src="filename.jpg?<?php echo time() ?>">
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

bokehman wrote:Adding all those headers won't help. All you need to do is the following with the image element.

Code: Select all

<img src="filename.jpg?<?php echo time() ?>">
Hey bokehman... that's a good idea... but to make things easy I went ahead and changed the inserted filename to include time().

Hence, filename-1883748273.jpg... and it seems to be working perfectly now since every uploaded file has a new filename now. Thanks so much for your help guys!!!
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Are you storing the actual image in the database or a link?

Most browsers will do a modify check on the image file and if you are storing the image data in the database the modify data is probably never changing. So the new image will never be downloaded as the server reports back there hasn't been a change causing the browser to use the cached image.

And if you are storing image data in the data base that is very bad practice. It is many times slower when it comes to accessing the image than if you store the image in a disk directory and the image name in the database. Plus you run into all kinds of weird and unforeseen complications when storing binary data in a database.
Post Reply