How do we clear the cache in our website using php?
Moderator: General Moderators
How do we clear the cache in our website using php?
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?
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:
Put this a the top of your script, before any output has been sent to the browser:
Code: Select all
header("Pragma: no-cache");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? Thanksnickvd 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.
-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
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...
Read it through, it should highlight all the do's and dont's to control the cache...
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Try settings in the header() function. It was posted as a response to your original post.
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
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...Everah wrote:Try settings in the header() function. It was posted as a response to your original post.
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);
?>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.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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/
Not compliant!!!!!
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>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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!
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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');