0

Before you mark this question as a duplicate, please note that I have seen similar questions on Stack Overflow, but none of them have been able to solve the issue for me.

I have a PHP script that creates a custom directory listing here:

http://snaps.vertigofx.com/index.php

These particular folders are for storing images. I have found that I will often I will be click an image to view it, and while it is open I will upload a new image. When I navigate "back" in my browser to get the newly uploaded image, I then also have to refresh the page for it to show up in the file listing.

Is there any way, either through PHP, Javascript, or even a Firefox setting/extension, that I can force the listing to refresh when I navigate back? (Please note that I want this to be site specific, so I don't want to use a browser setting that will make this effect global)

I have tried adding this to the PHP script, as it was the accepted answer on a similar question, but it did not work:

  header("Cache-Control: private, must-revalidate, max-age=0");
  header("Pragma: no-cache");
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // A date in the past

This works:

While the question at How to force page not to be cached in PHP? is not really a duplicate of my question (the asker on that post was referring to a single page whereas I need a solution that covers multiple pages), I was able to find a solution there. It's the cache-killer headers like I tried before but with a few more lines:

header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // HTTP/1.0
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

Confession: While working with this, it seems that changes to the code only seemed to reflect in the browser after actually deleting the file from the server, attempting to access it in the browser, and then uploading the new file. Simply overwriting the file didn't work for some reason. Because of this, it's very possible that my previous attempts at this may have very well been valid solutions that I just thought weren't working. I apologize for "wasting" anybody's time, and appreciate the help.

Community
  • 1
  • 1
vertigoelectric
  • 1,307
  • 4
  • 17
  • 37
  • This is not a duplicate of that question. That person wants to know how to force a refresh of a *specific* page, which is why the solution to having a header redirect to that page could work. I'm not working with a single specific page. However, I will play with the header redirect trick and some path variables to see if it can lead me to a solution. – vertigoelectric Feb 10 '14 at 04:20
  • I found a solution that worked, and it is on that page. I'll update my post to include the solution... – vertigoelectric Feb 10 '14 at 04:37

1 Answers1

0

You can create an <a> link with href back to your directory listing page with a random GET parameter at the end.

    <a onclick="window.location='http://snaps.vertigofx.com/index.php?p=' 
        + Math.random().toString(36).slice(2);return false;" 
        href="#">back to custom directory</a>

See HTML link that bypasses cache?

Or you can insert JavaScript on your directory page to use the window.location.reload(forceGet) method which takes a boolean parameter where, if set to true, forces the browser to reload from the server rather than the cache. You can call this when the page is loaded using the back button on your browser, but you need to make it so it only loads once. You can use the hash (#) for this:

    window.onload = function() {
        if(!window.location.hash) {
            window.location = window.location + '#updated';
            window.location.reload(true);
        }
    };
Community
  • 1
  • 1
transcranial
  • 381
  • 2
  • 3