How can i delete browser's cache?

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
kostasps
Forum Commoner
Posts: 26
Joined: Tue Jan 13, 2009 6:20 am
Location: Thessaloniki Greece

How can i delete browser's cache?

Post 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
Mephisto_ger
Forum Newbie
Posts: 1
Joined: Wed Feb 11, 2009 1:22 pm

Re: How can i delete browser's cache?

Post 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
kostasps
Forum Commoner
Posts: 26
Joined: Tue Jan 13, 2009 6:20 am
Location: Thessaloniki Greece

Re: How can i delete browser's cache?

Post 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
kostasps
Forum Commoner
Posts: 26
Joined: Tue Jan 13, 2009 6:20 am
Location: Thessaloniki Greece

Re: How can i delete browser's cache?

Post by kostasps »

Do you mean the "alt attribute" in the "img src" tag?
kostasps
Forum Commoner
Posts: 26
Joined: Tue Jan 13, 2009 6:20 am
Location: Thessaloniki Greece

Re: How can i delete browser's cache?

Post by kostasps »

Unfortunately it didn't work, i just tried it :cry:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How can i delete browser's cache?

Post 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).
kostasps
Forum Commoner
Posts: 26
Joined: Tue Jan 13, 2009 6:20 am
Location: Thessaloniki Greece

Re: How can i delete browser's cache?

Post 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?
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: How can i delete browser's cache?

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How can i delete browser's cache?

Post 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).
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How can i delete browser's cache?

Post 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.
kostasps
Forum Commoner
Posts: 26
Joined: Tue Jan 13, 2009 6:20 am
Location: Thessaloniki Greece

Re: How can i delete browser's cache?

Post 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 :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How can i delete browser's cache?

Post 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).
Post Reply