473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I prevent caching on my website?

I'm trying to prevent the caching of an image on my website. You see, the image is created dynamically, and when the user navigates throughout the site it changes each time. However, it's when the user tries to go back is the problem. The previous image is displayed instead of the new, dynamically created one. The correct image is created, it's just not being displayed. I have no idea where the previous image comes from, because it's destroyed right after its displayed and the buffer is cleared, so it must be the cache.

I have included these tags on every page of my website:

<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META http-equiv="expires" content="-1">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

The rest of my code is php and a little bit of javascript. The image is rendered using GD.
Any ideas?
Aug 11 '10 #1
7 1941
zorgi
431 Expert 256MB
To prevent only image from caching use little trick like this:

Expand|Select|Wrap|Line Numbers
  1. <img src="path/to/your/image/img.jpg?nocache=<?php echo time()?>">
  2.  
Aug 11 '10 #2
TheServant
1,168 Expert 1GB
When you say "dynamically created one" are you taking about a captcha?
Aug 11 '10 #3
@zorgi
Thanks a lot!! That worked wonderfully!! Could you per chance let me know why/how that works..?

@TheServant for the record, no I'm not talking about captcha, just an image that is generated based on what the user clicks on.
Aug 12 '10 #4
zorgi
431 Expert 256MB
@meekotherogue
time() gives different result once every second.

Lets say you have situation where time() gives you 1281651973. That would produce following path:

Expand|Select|Wrap|Line Numbers
  1. path/to/your/image/img.jpg?nocache=1281651973
  2.  
Very next next second that path looks like this:

Expand|Select|Wrap|Line Numbers
  1. path/to/your/image/img.jpg?nocache=1281651974
  2.  
Difference is minimal (only last digit) but reason enough for reloading.
Aug 12 '10 #5
londres9b
106 100+
Or you could use .htaccess if your server allows it.

It would be much easier and you wouldn't have to insert that piece of code on every single image.

Insert this on the .htaccess file:

Expand|Select|Wrap|Line Numbers
  1. <FilesMatch ".(jpg|jpeg|png|gif|bmp)$">
  2. Header set Cache-Control "max-age=0"
  3. </FilesMatch>
Aug 13 '10 #6
johny10151981
1,059 1GB
It can be done with php too
header("Cache-Control: max-age=0")
Aug 13 '10 #7
@zorgi that makes perfect sense! Thanks for the explanation, I like to know why my code works :P

@londres9b and @johny10151981 that seems like a good idea too, I appreciate your help :)
Aug 13 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Behzad | last post by:
Hi all I'am ASP programmer and I have built a site that users can upload and download files.All things store in a DB and everytime someone enters a page,the application requery the Db and shows...
29
by: Vjay77 | last post by:
I have a timer which triggers AxWebBrowser1 every 5 minutes to visit one of my websites. What I find out later in my websites logs, was, that it really visited my website only for the first...
10
by: dave Cheseldine | last post by:
Hi I want to stop a gd generated image from being cached. It is called into index.php file as follows: <image src = "http://www.daveches.co.uk/importimage/importimage.php?x=$randomNumber"...
7
by: mark4asp | last post by:
How can I prevent Caching of JavaScript and CSS files ONLY when I deploy a new application? I only want to force a refresh the first time the client uses the new build. For instance, I'm told I...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I force a reload from the server/prevent caching?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.