Page 1 of 2
How do we clear the cache in our website using php?
Posted: Fri Jul 14, 2006 6:38 am
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?
Posted: Fri Jul 14, 2006 9:53 am
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:
Posted: Fri Jul 14, 2006 1:47 pm
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.
Posted: Sat Jul 15, 2006 1:21 am
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
Posted: Sat Jul 15, 2006 1:59 am
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...
Posted: Sat Jul 15, 2006 2:03 am
by RobertGonzalez
Try settings in the
header() function. It was posted as a response to your original post.
Posted: Sat Jul 15, 2006 4:31 am
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.
Posted: Sat Jul 15, 2006 9:15 am
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...
Posted: Sat Jul 15, 2006 10:00 pm
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.
Posted: Mon Jul 17, 2006 2:35 pm
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!!!!!
Posted: Tue Jul 18, 2006 12:54 am
by RobertGonzalez
I would set the headers server-side instead of client-side. Make for cleaner coding and compliant HTML.
Posted: Tue Jul 18, 2006 2:02 am
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!
Posted: Tue Jul 18, 2006 2:05 am
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.
Posted: Tue Jul 18, 2006 2:08 am
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!
Posted: Tue Jul 18, 2006 2:15 am
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');