0

I would like to "force-load" an instance of a page every time it's accessed.

The website in question is a "build your custom product"-kind of site and uses a multi-step form to guide customers through the steps of the product building. The client wants the site to be such that every time a user access the site, whether by direct url or by clicking on the "back" and "forward" buttons on the browser, it must refresh. In other words, no caching (right?).

I've tried to use the solutions I found on SO and on other websites, which mainly consisted of

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

But that didn't work.

So I tried my own solution:

$location = "/fabriquer/";
if(isset($_COOKIE['first_load']) && $_COOKIE['first_load'] == "true") {
    setcookie('first_load', 'false');
    header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location);
} else {
    setcookie('first_load', 'true');
}

But that also seems to not be working properly. When I press the back button on my browser to get back on that site, the refresh does not happen, but the cookie is set to true/false in the second solution.

Any ideas?

2
  • So you're trying to force the users browser to never cache the page? Can you describe more of what 'doesnt work' explicitly means?
    – castis
    Commented Apr 29, 2015 at 22:31
  • I updated my question with more details. Commented Apr 29, 2015 at 22:41

2 Answers 2

0

The only way you can get the browser to nto cache the page is to change the url every time. For instance append a random number or datetime as a param

$url = $url.'?ver='.rand(1,1000);

Add this to every url as you go from one page to another. I add a version number on mine. That way, whenever I update the site, everyone gets fresh copies.

0

As Joe mentionned in the comments earlier today, and another [deleted?] comment, my question was indeed a duplicate to the one asked here.

It turns out that I was doing it wrong and that deleted comment put me on the right path, until I saw Joe's comment.

The deleted [approximately] comment stated:

You should merge the two Cache-Control lines into one, as the second one overrides the first one.

Whoever wrote this, thank you.

So I just looked at the code in the original question (linked above) and found the following piece of code that fixed my problem.

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

I then tested on my browsers (cleared the cache properly and used incognito mode just to be safe) and it worked perfectly. I will not "answer my own question" but I wanted to provide my reasoning. This question is indeed a duplicate and the answer was found in BalusC's answer.

Thank you for your help!

Not the answer you're looking for? Browse other questions tagged or ask your own question.