Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,564 members, 7,816,380 topics. Date: Friday, 03 May 2024 at 10:26 AM

Question 2: How To Prevent An Image From Storing In The Cache - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Question 2: How To Prevent An Image From Storing In The Cache (924 Views)

How To Configure W3 Total Cache Plugin and increase your website speed. / Why Bcrypt Is The Most Secure Way Of Storing Passwords than Blowfish / Importing Edited Image From Photoshop To Coreldraw (2) (3) (4)

(1) (Reply) (Go Down)

Question 2: How To Prevent An Image From Storing In The Cache by Nobody: 6:36pm On Feb 26, 2009
How do you prevent images from storing in the browser cache of your website.
The solution must be in coding. I mean you cannot keep telling all your site visitors
to clear their cache and stuffs like that to see updated image. At the same time
you cannot keep renaming your images and tampering with code or dbase just
to show newly updated images.
Any website language is welcome. In fact i encourage it. But i guess most people
here can read javascript / php properly.

As usual i will post my answer after if nobody gets it right. No prizes this time.
I mean, this one is way too eaazzzy.
Re: Question 2: How To Prevent An Image From Storing In The Cache by yawatide(f): 6:53pm On Feb 26, 2009
But of course, "I mean, this one is way too eaazzzy" to you because you are the one asking the question.  Who are you kidding man?  tongue

Sha, to answer your question, here is how I have handled this in recent years:

I tack on a timestamp name/value pair like so:
<img src="images/bla.jpg?ts=222" />

Thus, the timestamp (generated by a function of your backend language of choice) will change with each page refresh and so is guaranteed to be the latest one.  Also, just for kicks, you could even tack on a "version" for tracking purposes, like so:
<img src="images/bla.jpg?ts=222&version=33" />

By the way, the above also applies to other files (js, css, etc) as well. 

Granted, I am sure there are other ways of doing this, I have used this technique for at least 5 or 6 years and it works like a charm.

I find this question quite interesting. Hopefully, there are more like this to come smiley
Re: Question 2: How To Prevent An Image From Storing In The Cache by Nobody: 7:54pm On Feb 26, 2009
Iight yawa, that was a very nice thread. And that method is efficient too.

I have another technique i use (pure server sided), i have been using it since the days
of perl before php came out. I can be used for css, jscript, image, flash and other
embeddable contents.

I choose not to use javascript today.

index.php
<title>No Cache</title>

<img src="image.php?jlo.jpg" align="middle" width="250">


image.php
<?php
session_start();
header("Cache-Control: no-cache, must-revalidate"wink; // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"wink; // Date in the past
header("Content-type: image/jpeg"wink;

$filename=$_SERVER['QUERY_STRING'];
$im = imagecreatefromjpeg($filename);
imagejpeg($im, null, 100);
imagedestroy($im);
?>


This example is for rendering jpeg images. Though you can manipulate it to show other image formats.
I do not wish to deviate from the topic to php image functions.

header("Cache-Control: no-cache, must-revalidate"wink; which to the browser means, please do not cache.


How to know if the image is not really cache. After loading the index page. Quietly go to the server and upload another image
in its place, and do normal refresh for the browser, you will see the new image come up immediately or you will have your money back.

If you have internet explorer, then please check the cache, the image will not be stored there either.

The online sample is http://mwebng.net/demos/no-cache

The correct title of this thread should be: how to use headers to prevent browser from cacheing images.

That is done then, next php discussion comes up soon enough.
Re: Question 2: How To Prevent An Image From Storing In The Cache by yawatide(f): 8:04pm On Feb 26, 2009
I choose not to use javascript today.
Haaa-le-lu-jaaaaah! tongue

Your code works, yes, but it is very specific to PHP.  I would have posted same but didn't because of one of the things you wrote above:
Any website language is welcome. In fact i encourage it.

By that statement, the implication is that the solution should be generic (hence my timestamp solution) unless of course you are implying that I could copy the code you have above, paste it in a JSP page and it would work flawlessly (something I doubt) wink

Again, good question.  Keep them coming.
Re: Question 2: How To Prevent An Image From Storing In The Cache by Nobody: 9:18pm On Feb 26, 2009
My true web-programming powers lie in perl/php/mysql rather than javascript (forget about type-casting).
Well, the solution in question has to do with headers and can be written in any server-sided web language.
Re: Question 2: How To Prevent An Image From Storing In The Cache by yawatide(f): 9:39pm On Feb 26, 2009
Yes, but not the way you have it. You could have still written it generically. Again, reference your direct quote above.
Re: Question 2: How To Prevent An Image From Storing In The Cache by Nobody: 11:03pm On Feb 26, 2009
Rewritting? Once i have passed the message across and it is working well -  that is it. I cant write perfect code for copy-pasters. The version i use in reality is very very different from this one. When i make tutorials, it is for people to learn especially the principles, work on it and get better.

If anyone care about rewritting it, then that is their luck. I am satisfied with my post. If you can modify it to be better, then do so and post it here.
And i am alwayz open to questions. If anyone is not clear, please ask o.
Re: Question 2: How To Prevent An Image From Storing In The Cache by yawatide(f): 3:38am On Feb 27, 2009
The version i use in reality is very very different from this one.

Then you are doing the people who are learning from you a great disservice.
Re: Question 2: How To Prevent An Image From Storing In The Cache by Nobody: 6:42am On Feb 27, 2009
The purpose of this tutorial has been satisfied. If i post the full code here, the newbies will curse me 'cos they wont be able to read my codes.
But like I said the main lesson should have been learnt, those that are wise let them learn from it and develop themselves better.
If anyone wants my full library, then will need to steal my laptop or my portfolio and my website.
Re: Question 2: How To Prevent An Image From Storing In The Cache by yawatide(f): 6:55am On Feb 27, 2009
hmmmmmmmm
Re: Question 2: How To Prevent An Image From Storing In The Cache by Nobody: 7:02am On Feb 27, 2009
** Locks threads, and disabled notification. Cya later. Hope someone has answered my next question**

(1) (Reply)

Help / Blog To The Bank 2010 / H

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.