How do we clear the cache in our website using php?

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

joseph
Forum Newbie
Posts: 24
Joined: Mon Jul 03, 2006 6:15 am

How do we clear the cache in our website using php?

Post by joseph »

Do we use php to clear the cache of our website so that everytime the user visits the website is updated? or do we use html or javascript? anybody here please teach me how this is done?
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

You can send the "Pragma: no-cache" header, which should tell the browser to fetch a new copy.

Put this a the top of your script, before any output has been sent to the browser:

Code: Select all

header("Pragma: no-cache");
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Just to clarify something.. There is NO way for your website to "clear" the browser cache.. all you can do is force the browser to ignore what's in their current cache and load the page from your server.
joseph
Forum Newbie
Posts: 24
Joined: Mon Jul 03, 2006 6:15 am

Post by joseph »

nickvd wrote:Just to clarify something.. There is NO way for your website to "clear" the browser cache.. all you can do is force the browser to ignore what's in their current cache and load the page from your server.
Yes, is there a code that will set the cache to expire so that everytime the users browsing the website will have to load the site cache again? Thanks
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I've never done it myself (as of yet i haven't seen the need to...), but after about 2 minutes of searching, i found: http://www.mnot.net/cache_docs/

Read it through, it should highlight all the do's and dont's to control the cache...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try settings in the header() function. It was posted as a response to your original post.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

If you're looking for a way to force a browser to (re)load a flash movie, you can also append a random get variable to the url.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Everah wrote:Try settings in the header() function. It was posted as a response to your original post.
Everywhere i've read (well, on the page i linked to) said that the pragma header is not foolproof and wont always work. Unless they were being specific to using pragma in a meta tag...
joseph
Forum Newbie
Posts: 24
Joined: Mon Jul 03, 2006 6:15 am

Post by joseph »

Code: Select all

<?php
 Header("Cache-Control: must-revalidate");

 $offset = 60 * 60 * 24 * 3;
 $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
 Header($ExpStr);
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

There, it says that these codes will allow the webpage to expire the cache after 3 days I wonder if this really works. I placed this on top of any other codes.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

IE is a bit buggy about Pragma: no-cache. You need to put another header at the bottom after </body> and before the </html>
http://support.microsoft.com/kb/222064/

Code: Select all

<HTML>

<HEAD>

<META HTTP-EQUIV="REFRESH" CONTENT="5">
<TITLE> Pragma No-cache </TITLE>
</HEAD>
<BODY>
This is an example of where to place the second header section<br>
so that the "Pragama, No-Cache" metatag will work as it is supposed to.<br>
</BODY>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</HTML>
Not compliant!!!!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would set the headers server-side instead of client-side. Make for cleaner coding and compliant HTML.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Agreed about cleaner and compliant. But as IE is so buggy, does server side do it in a way that IE will reliably recognise? 90% of visitors to my site unfortunately use IE, so the need to code for reliabilty comes over compliance in this instance.

I use cache control for admin areas where changes have been made and the user has gone back to the page displaying what it looks like. They need to see the fresh page not the cached one from before they made changes. For the sake of clean, compliant code what's the best way of doing that? Noted you could add a random variable to the url each time - but that's not very clean either!
Last edited by andym01480 on Tue Jul 18, 2006 2:06 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do you write non-compliant code so it works in the non-compliant browser? Sometimes you have to. When you don't have to, don't do it. This is my opinion.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Wow! Both writing at the same time - I edited my post!

Yes! nice not too - but cache control is an area where we probably have to be non compliant to be reliable!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I totally bit this little snippet from phpBB...

Code: Select all

// Work around for "current" Apache 2 + PHP module which seems to not
// cope with private cache control setting
if (!empty($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache/2'))
{
	header ('Cache-Control: no-cache, pre-check=0, post-check=0');
}
else
{
	header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
}
header ('Expires: 0');
header ('Pragma: no-cache');
Post Reply