Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

PHP/HTML: Smart page caching (used cached HTML only on back & forward button click but force pages to re-load on direct links)

Ok, I'm pretty sure the question is not new but due to the rather complicated nature of the issue I failed to produce a lucky query. Basically, I want my script to partially cache my forum pages to speed up navigation, preserve incomplete forms should any accident take place and prevent unnecessary topic view increments, which are constant MySQL calls. I know most popular forums use this sort of caching, phpbb for one, but I simply can't get my head around how they pull it off. I've messed with headers a bit, granted I still have a very vague understanding of how it works exactly, but no matter what I did I could only go as far as making completely static pages, which are only updated on forced reload, never on hyperlinking. So, what's the magic formula, anyway? Surprisingly, most people are looking for ways to turn the caching completely off with something like this:

header("Cache-Control: no-cache, no-store, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");

But what do I do to achieve my end? I know it can also be done with buffers or even .htaccess, but I'm pretty sure neither is a typical solution. I was thinking maybe it's done with a condition similar to:

if(!isset($_SERVER['HTTP_REFERER'])) {
// cache headers
}

...but I really don't know if this should work, I suppose if cache headers were defined ones then next time the browser will load a cached copy of the page regardless whether present in the condition or not. So, as you can see it's not just the problem itself, it's also my lack of understanding of this particular subject. Any help would be appreciated. And, please, no javascript.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, I finally found it:

function cachePage() {
    $date = gmdate('D, d M Y H:i:s');
    header('Pragma: no-cache');
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: ' . $date . ' GMT');
    header('Last-Modified: ' . $date . ' GMT');
}

It does exactly what I need except I still don't know which part does the magic trick precisely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...