I'm making a website where users select one of the four options - A, B, C, or D (for simplicity) and press a button to be redirected to a different page (page 2) that uses the selection to display one of the four images at the top.

The selection is stored in a text file using PHP.

Page 2 will read the text file and display the picture based on the value read.

The problem is if the user had previously chosen option A and then presses the back button and chooses option C, the first time Page 2 loads, it will still display the picture for option A. Only after refreshing the page once it will load the correct picture for option C.

I tried to prevent IE from caching the page.. but it was no use.

    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="-1">

Note: The website only has to work in IE so no need to make it compatible with other browsers...

Anyone know what am I doing wrong here? Or how to prevent the browser from remembering the previous information?

Dani AI

Generated

Likely causes: the browser is serving a cached copy of the final page or the image resource (IE can be aggressive), or the workflow is racing — the disk write on the USB server hasn’t completed before the redirect. was correct to suggest inspecting headers, and ’s .htaccess idea is valid for controlling caching at the server level. The onbeforeunload auto-submit approach mentioned by is fragile and makes timing/caching issues harder to debug.

Practical checklist and fixes (apply at the server side, not only with meta tags):

  • Force no-cache response headers from PHP at the very start of the final page so IE won’t reuse a cached response:
<?php
// place this before any output in final.php
header('Expires: 0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
?>
  • If the same image filename is reused but its content changes, force a fresh fetch by adding a version/query string tied to the selection or a timestamp (this avoids relying on cache headers for image resources):
<img src="banner.png?v=<?=time()?>" alt="banner">
  • Make the save+redirect robust: write atomically and redirect only after the write completes (use file_put_contents(..., LOCK_EX) or equivalent), then call header('Location: final.php'); exit;.

Debugging tips: use IE’s F12 Network tab (or Fiddler as suggested by ) to confirm whether final.php or the image is returned from cache (status 304 or “from cache”). For the USB host, add temporary logging or a short debug pause to confirm the file contents were updated before redirect. For a more reliable design long-term, pass the selection via POST/GET or session instead of round‑tripping through a shared text file on removable media; that eliminates both cache ambiguity and file I/O races.

Recommended Answers

All 11 Replies

Did you inspect the header to make sure you implemented the code properly. There are quite a bit of online sites that let you see the results of the header. Check that, just to rule it out as an issue.

Did you inspect the header to make sure you implemented the code properly. There are quite a bit of online sites that let you see the results of the header. Check that, just to rule it out as an issue.

Hmm, the problem is I'm not running this on a publicly accessible server. It will be run locally with a USB web server.

You can also download a tool called Fiddler. I am not an expert with it, but have used it to diagnose issues.

I set timeouts in .htaccess to 5secs for anything that needs it, is the usb server a wamp/lamp system?

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 5 seconds" 
ExpiresByType image/gif "access plus 5 seconds" 
ExpiresByType image/jpg "access plus 5 seconds" 
ExpiresByType image/png "access plus 5 seconds"  
ExpiresByType application/x-javascript "access plus 5 seconds" 
ExpiresByType text/css "access plus 5 seconds" 
ExpiresByType text/javascript "access plus 5 seconds" 
ExpiresByType image/x-icon "access plus 5 seconds" 
</IfModule>

It's WAMP system. http://www.usbwebserver.net/en/index.php

I also don't have a .htaccess file that I can edit (at least not that I can see)! The USB server has a total of three .htaccess files & they're all for PHPmyadmin...

create a .htaccess file in the html_root (exact location depends on your config, wherever your index.php site root is) folder on the thumb drive,
may be all thats required,
(sometimes -not very often- things work easy),
there is minimal documentation on the site, but it appears that ANYTHING, can go in the thumbserver's html root . My AMP test stack is on my server, but I can see myself grabbing a copy of this thing, just to nhave it

You should have the proper tool for this...Try to fine with your own..

playing with the USBWebserver, .htaccess works if dropped in F:\USBWebserver v8.5\8.5\root

liking it, very few things WORK straight out of the box, this does

I did that, but it's still displaying older version....
.
.
.

I'm not sure if it's the cache that's the problem or my code... :S

What I'm doing is the following:

I have a form.php file that takes in user input (checkboxes and text input).

Whenever the page is being "unloaded" (javascript), the page will submit the form by sending the info to process_script.php. That script will write the info in text files.

After writting, it will redirect the user to the final.php which will pull the info from the text files and display the page. (include() function of PHP)

.
.
.
.

Does any of that seem to be the problem?

if you think it is the code, post the code, there might just be an even simpler fix

Data Input method: there are more like these but this is the general idea...

<input type="radio" class="check" name="gen_company_name" value="other" <?php $data = file_get_contents("http://localhost:8080/core/data/form_generation/gen_company_name.txt"); if ($data == "other") echo "checked"; ?> />Blank<br/>

then to send the data to the script that saves:

<script type="text/javascript">    
        window.onbeforeunload = confirmBrowseAway;

        function confirmBrowseAway()
        {
            document.forms["gen_form"].submit();
        }

        window.onload = function()
        {
            document.forms[0][0].focus();
        }
    </script>

Then to save the inputted data:

<?php
$sfiles[1] = "gen_signature_third_line_feature.txt";                $sinput_name[1] = "gen_signature_third_line_feature";

    for ($si = 0; $si <= 1; $si++)
    {
        if ($_POST[$sinput_name[$si]] == "yes")
        {
            $data = $_POST[$sinput_name[$si]];
            $file = fopen ($directory.$sfiles[$si], "w");
            fwrite ($file, $data);
            fclose($file);
        }
        else
        {
            $data = "no";
            $file = fopen ($directory.$sfiles[$si], "w");
            fwrite ($file, $data);
            fclose($file);
        }
    }
?>

& finally, to retrieve the data:

<p class="RE">RE:</p>
<p class="subject">
    <?php include($serverdir."edited_gen_re_line1.txt"); ?><br/>
    <?php include($serverdir."edited_gen_re_line2.txt"); ?><br/>
    <?php include($serverdir."edited_gen_re_line3.txt"); ?><br/>
</p>

<br/>

<?php include($serverdir."edited_gen_body.txt"); ?><br/>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.