views:

53

answers:

2

Hi, We have an application in PHP. The problem comes when we make updates and changes in this application. Sometimes, the users don't get the last version of the application (HTML and javascript are mainly changed) because of the browser cache. Once we realized about it, we were able to include the next code in our application (We execute it before any HTML is sent to the browser)

function clean_cache(){
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header("Cache: no-cache");
    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");
}   

But still we have some users that have older versions of our application. We read that a possible solution is to use a javascript asking the server via ajax if there is an update. If so, force a reload. If this is the best way to solve our problem : How can we implement this solution? If you have any other possible solution we will be very happy to know it.

Thanks

A: 

store the last update in a variable and use ajax to do a ajax request every minute and to check wheterh the update time differs from the stored update time in the variable. if so do a reload with window.location = "the new location";

EDIT:

it should look like this: (but i didn't test it so i don't guarantee it to work)

var updateTime = '<?php echo $lastUpdateTime;?>';
$(document).laod(function(){
    var reload = window.setInterval("request()", 60000);
});

function request(){
    $.get("last_update.php", function(data){
        if(updateTime != date) window.location = "redirect_to.php";
    });
}

EDIT 2: This is HTML for including jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; 

this "redirect_to.php" is the file you redirect the window to. so if you want to stay on the same site just put the url of the site there.

<?php $lastUpdateTime;?> was a bit wron inline PHP. i corrected it to <?php echo $lastUpdateTime;?>

last_update.php is just a page that returns the last update. how you keep track of the last update is up to you.

ITroubs
I don't understand well how to do that.
pepersview
see my edit. btw you need to include the jquery source to use my example!
ITroubs
Thank so much for clarify my doubts. What last_update.php is suppose to have inside? We dont keep track of updates, we just make the changes in the application that are requested.
pepersview
then you should start to keep track of your changes at least in the way that you put the date and time of the last changin into that last_update.php
ITroubs
Should then we create a last_update.php file an write in it the date and time? Just this? Thanks
pepersview
this would be the minimal solution. <?php $lastUpdateTime = '2010-10-05 15:30:25'; if(!empty($_GET['echo_it']) echo $lastUpdateTime;?> and call it like this `$.get("last_update.php?echo_it=doesntmatter",` and do an include of the last_upload.php in your php where you put the jquery code into
ITroubs
A: 

IE caches files from the same URL, let's say you're loading js-files from your html header, you can try and randomize a query number in the url, just to force IE to interpret this as a new URL and reload it.

Example:

<?php 
    $random = rand(1, 100000);

?>
<head>
<script type="javascript/text" src="../js/foo.js?v=<?php echo $random;?>"></script>
</head>
...

This will output something like

<script type="javascript/text" src="../js/foo.js?v=5"></script>

if you make sure the URL is "dynamic" IE will be forced to reload it.

Stefan Konno
The problem is not only with IE, we also had it in Mozilla.
pepersview