Page 1 of 1

How can i delete browser's cache?

Posted: Wed Feb 11, 2009 12:58 pm
by kostasps
Hallo friends,

Once again i am gona need your advices :)
Well i am asking if there is any script or function with which i can delete browser's cache so it won't keep photos in his memory.

I am asking this because i am dealing the following issue:
I am making a web page in which i have three php files.
These are the: images.php, image_edit.php and image_insert.php
Now in the images.php file the servers reads from a DB the path of the folder in which there are the photos we want to load. Also it has a form with two links - edit and delete photo - and a buton about adding new photos in the specific folder, and this form redirects to the image_edit.php file.
The image_edit.php file shows 1 of 3 forms (weather the user clicked on one of the two links or pushed the button) and it asks for some details (e.g. confimation of the delete - if the user clicked on the "delete" link).
And last there is the image_insert.php file that makes all the changes that the user asked.

Now everthing works fine meaning except one thing:
New photos can be uploaded (both to the folder and the DB) and can be shown to the page
Already existing photos can be deleted (both from the folder and the DB) and will not be shown any more
And last already existing photos can be modified - meaning that if the user want can upload another photo in the place of an already existing and both in the DB and the folder the new photo will take the name of the old one and the old one will just disappear.

Now the problem is in the third situation (when editing) and as i said before while the new photo is uploaded succesfully in the folder and has taken succesfully the name of the old one and from the other hand the old one disappear both from the DB and the folder, unfortunatelly the new photo doesn't appears in the page but instead the old one still remains in it's place.
Only if i close the browser and open it again only then the browser refresh it's data.

So i gess that this issue is dealing probaply with browser's cashe (smth that i can't understand if we consider that i move from images.php to images_edit.php file and then to images_insert.php which after it finish whatever has to do it redirect's me back to images.php).

It would be very helpfull if somebody could help me

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 1:26 pm
by Mephisto_ger
in my opinion there is no way to delete the browser-cache via php, and neither by js

but one thing you could try(it worked for me once) you have the option to use an alt-attribute, so if you change the alt-attribute the browser thinks its an new image an reloads...

please reply if it worked...

excuse my bad english btw

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 1:28 pm
by kostasps
Mephisto_ger,

Thank you for your answer.
Could you give me an example of this alt-attribute? because i can't figure it out

Thank you again

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 1:30 pm
by kostasps
Do you mean the "alt attribute" in the "img src" tag?

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 1:33 pm
by kostasps
Unfortunately it didn't work, i just tried it :cry:

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 1:36 pm
by John Cartwright
This technique is commonly used to reload captcha images on ajax requests.

In jquery (borrowed from one of my sites)

Code: Select all

var d = new Date();
$('#captcha').attr('src', '/service/captcha/?time='+ d.getTime());
The point being, the browser will think its a new image because ?time= will change over time.

Can be implemented in either PHP or JS (I would prefer to keep this on the client).

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 1:46 pm
by kostasps
John Cartwright ,

Thank you too for your queek answer.
Forgive me but i don't know how to implement it in PHP and that's because i know few from JS :cry:
Could you please handle the implementation?

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 2:05 pm
by Ziq
It will be something like this

Code: Select all

 
<img src="path/to/image.jpg?time=<?php echo time(); ?>" alt="something">
 
But if you will do this you can have some problems with image search engines (like http://images.google.com/) They can create a lot of useless traffic. Because they also will think that it's a new image. It's just theory I never had this problem.

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 2:24 pm
by John Cartwright
Ziq wrote:It will be something like this

Code: Select all

 
<img src="path/to/image.jpg?time=<?php echo time(); ?>" alt="something">
 
But if you will do this you can have some problems with image search engines (like http://images.google.com/) They can create a lot of useless traffic. Because they also will think that it's a new image. It's just theory I never had this problem.
I doubt that would be an issue.

Alternatively, you could create a script to output the image itself along with no cache headers (assuming the browsers will play ball).

Re: How can i delete browser's cache?

Posted: Wed Feb 11, 2009 2:31 pm
by requinix
As an alternative, don't use the same name for the new image. It's a different image so why should it have the same name? Similar name, maybe. You don't have to name the image only after (eg) a username: username_uploadtime.ext can work too.

If not, how about tracking the image revision or upload time? Browsers will cache stuff if you give the same URL. The src attribute could look like

Code: Select all

src="/path/to/images/image.png?1"
meaning that this image.png was the first one uploaded. ?2 for the second, ?3 for the third, and so on.

Re: How can i delete browser's cache?

Posted: Thu Feb 12, 2009 2:54 am
by kostasps
Ziq wrote:It will be something like this

Code: Select all

 
<img src="path/to/image.jpg?time=<?php echo time(); ?>" alt="something">
 

My friends,
This script helped me solve this problem succesfully and now i am back to bussiness :D
Once again i want to thank you all of you for your quick and precius advices.
Cheers :)

Re: How can i delete browser's cache?

Posted: Thu Feb 12, 2009 10:55 am
by John Cartwright
tasairis wrote:As an alternative, don't use the same name for the new image. It's a different image so why should it have the same name? Similar name, maybe. You don't have to name the image only after (eg) a username: username_uploadtime.ext can work too.

If not, how about tracking the image revision or upload time? Browsers will cache stuff if you give the same URL. The src attribute could look like

Code: Select all

src="/path/to/images/image.png?1"
meaning that this image.png was the first one uploaded. ?2 for the second, ?3 for the third, and so on.
Good idea. I would follow this users advise since the cache will only be updated when neccesary (and not every request).