PHP: Prevent image caching, new/uploaded avatars

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
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP: Prevent image caching, new/uploaded avatars

Post by JAB Creations »

How can I prevent images from being cached in the browser using PHP?

The scenario is simple: I have created the ability to upload an avatar image. However when I upload different images I still see the previous image. I am using the same file name however it's predicable and creates less server load. So I'm only interested in preventing the image from being loaded in the cache only on the following condition: $_FILES['image_avatar']['name'] != "". I would not mind in example trying to use a random generated path that the server would still point to the same image though since the browser would see a different path it would then not bother with the cached version.

I did try this though without any luck...

Code: Select all

header('Expires: Tue, 22 May 1988 03:20:00 GMT'); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
header('Cache-Control: no-store, n0-cache, must-revalidate'); 
header('Cache-Control: post-check=0, pre-check=0', false); 
header('Pragma: no-cache');
Suggestions please?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: PHP: Prevent image caching, new/uploaded avatars

Post by panic! »

a quick solution would be:


the_real_file.jpg?id=39390432


39390432 being a random number or..


/image/39390432/the_real_file.jpg

and use a rewrite rule in an .htaccess file:

Code: Select all

 
RewriteEngine On
RewriteRule image/(.*)/(.*)   /images/$2.jpg
 

obviously I don't know your directory structure so you'd have to amend this to reflect your directory structure.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP: Prevent image caching, new/uploaded avatars

Post by JAB Creations »

I'm not seeing how the server will know which file is the image requested. What does the Apache code do in plain English?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: PHP: Prevent image caching, new/uploaded avatars

Post by panic! »

A please or thank you would be nice..Manners cost nothing...
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP: Prevent image caching, new/uploaded avatars

Post by JAB Creations »

I consider myself a bit more keen to ensuring people who answer my questions are thanked. Sure now looking back I should have added "Could you please clarify..." to the post. I do thank you for taking the time to answer it however if I don't comprehend the answer I'll try to communicate for a clarification first. It was not my intent to be rude in any sense. I usually follow up most final posts with examples or at least a connect the dots sort of reply so others viewing the archive threads I have started or participated in can comprehend what I did without having to second guess...I reserve the thanks once at least some two-way communication has been successful.

So could you 011100000110110001100101011000010111001101100101 clarify what Apache is doing as it's syntax is too minimalistic for me to comprehend thus far at this moment in time?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: PHP: Prevent image caching, new/uploaded avatars

Post by Zoxive »

JAB Creations wrote: So could you 011100000110110001100101011000010111001101100101 clarify what Apache is doing as it's syntax is too minimalistic for me to comprehend thus far at this moment in time?

Code: Select all

RewriteEngine On
RewriteRule image/(.*)/(.*)   /images/$2.jpg
All that is doing is routing to the image file, but it has a random generated number in between to trick the browser into thinking its a different file.

The only thing is the regex for that is incorrect. Unless your file names are myname.jpg.jpg

Code: Select all

RewriteEngine On
RewriteRule image/(.*)/(.*)\.jpg  /images/$2.jpg
## we only want .jpg once
But i would just recommend the other way. Less work to do.

my/folders/to/image.jpg?v=30294

I use this solution for most of my images. Because i tell the browser to cache them for a month, so the "Version" (?v=(num)) number doesn't change, I change them when i update the images.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: PHP: Prevent image caching, new/uploaded avatars

Post by panic! »

oh yeah, two file extensions: my bad! Thanks for pointing that out Zoxive.
'JAB Creations' definitely deserves his most peculiar award.
Post Reply