Php 按下后退按钮时强制重新加载/刷新

Php 按下后退按钮时强制重新加载/刷新,php,mysql,codeigniter,Php,Mysql,Codeigniter,我会尽力解释的 我有一个应用程序,在我的视图页面中显示50多个项目。用户可以单击单个项目并转到更新页面以更新项目信息。除了用户完成单个项目信息的更新并点击浏览器上的“后退”按钮进入上一个查看页面后,一切正常。旧项目信息(更新前)仍然存在。用户必须点击“刷新”以查看更新的信息。这并不坏,但我希望提供更好的用户体验。有办法解决这个问题吗?非常感谢。好吧,您可以在更新页面上提供一个内置的后退按钮,将它们发送到新页面(例如链接,或者在页面上放置一些脚本,在每次点击页面时强制其提取数据,无论是新的印象还是

我会尽力解释的


我有一个应用程序,在我的
视图
页面中显示50多个项目。用户可以单击单个项目并转到
更新
页面以更新项目信息。除了用户完成单个项目信息的更新并点击浏览器上的“后退”按钮进入上一个
查看页面后,一切正常。旧项目信息(更新前)仍然存在。用户必须点击“刷新”以查看更新的信息。这并不坏,但我希望提供更好的用户体验。有办法解决这个问题吗?非常感谢。

好吧,您可以在更新页面上提供一个内置的后退按钮,将它们发送到新页面(例如链接
,或者在页面上放置一些脚本,在每次点击页面时强制其提取数据,无论是新的印象还是使用了后退按钮。

您可以在查看页面上发送HTTP标头,告知浏览器无法缓存页面。这意味着浏览器需要从每次访问服务器时,它都会显示最新信息

需要在发送任何其他内容之前发送标题,因此请将以下内容放在视图页面的顶部

header("Cache-Control: no-cache, must-revalidate");

我认为您必须使用JS才能实现这一点,因为PHP无法知道用户可以访问哪些浏览器控制

也许这会有帮助:

我在我的一些页面中使用了一个解决方案。将此添加到进行更改的页面中

window.onbeforeunload = function() {
     window.name = "reloader"; 
        }
这会在您离开这些页面时触发。如果进行了更改,也可以触发它。这样它就不会不必要地重新加载需要重新加载的页面。 然后在浏览器“返回”使用后要重新加载的页面上

这将在需要重新加载到的页面上触发重新加载。。。 希望这有帮助:) 哦,顺便说一句,这是在js中。

fb缓存真的很烦人。下面是一个简单的javascript方法:

window.onpageshow = function(evt) {
    // If persisted then it is in the page cache, force a reload of the page.
    if (evt.persisted) {
        document.body.style.display = "none";
        location.reload();
    }
};

在Safari 6和IE10中测试。

我使用jquery和php脚本强制加载backbutton。window.unload部分仅在firefox中需要。outcommented timeout 1秒和body append部分仅用于说明功能。window.location.reload(true)将给出与window.location.href=window.location.href相同的结果

jquery:

<script>
    $(window).unload(function() {});


        $.get('h.php',function(data){
            if($.trim(data)=='first'){
               // $('body').append(data)
               // setTimeout(function(){
               window.location.href = window.location.href;
               // },1000)
            }
            else {
                // $('body').append(data)
            }
        });

</script>

$(窗口).unload(函数(){});
$.get('h.php',函数(数据){
如果($.trim(数据)='first'){
//$('body')。追加(数据)
//setTimeout(函数(){
window.location.href=window.location.href;
// },1000)
}
否则{
//$('body')。追加(数据)
}
});
h、 php:

<?php session_start(); ?>
<?php 
if(!$_SESSION['reloaded']){
    echo 'first';   

    $_SESSION['reloaded']=1;
}
else {
    echo 'second';
    unset($_SESSION['reloaded']);   
}
?>

制作一个隐藏的复选框并使用下面的代码(coffeescript)


在每个页面中添加这两个函数

我使用以下四行PHP代码:

// any valid date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified right now
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: private, no-store, max-age=0, no-cache, must-revalidate, post-check=0, pre-check=0");
// HTTP/1.0
header("Pragma: no-cache");

密钥使用缓存控制头的“no store”子句。

这是我使用的解决方案:

<?php
session_start();
if (!$_SESSION['reloaded']) {$_SESSION['reloaded'] = time();}
else if ($_SESSION['reloaded'] && $_SESSION['reloaded'] == time()) { ?> 
<script> 
location.reload(); 
</script> 
<?php } ?>

location.reload();

上述解决方案都不适合我。 这个简单的解决方案有效

我曾尝试在用户单击“后退”按钮时强制浏览器再次下载页面,但没有任何效果。我发现现代浏览器对页面有单独的缓存,用于存储页面的完整状态(包括JavaScript生成的DOM元素),因此,当用户按“后退”按钮时,上一页会立即显示为用户离开的状态。如果要强制浏览器在“后退”按钮上重新加载页面,请将onunload=“”添加到(X)HTML正文元素中:

这将禁用特殊缓存,并在用户按下时强制重新加载页面 后退按钮。使用前请三思。需要这样做的事实 解决方案暗示您的站点导航概念存在缺陷


幸运的是,我们不必支持IE10以下的浏览器,因此,如果您愿意或有足够的特权只支持支持支持HTML5的浏览器,那么以下几点应该可以:

/*
 * The following is intentional, to force Firefox to run 
 * this JS snippet after a history invoked back/forward navigation.
 */
window.onunload = function(){};    

function formatTime(t) {
    return t.getHours() + ':' + t.getMinutes() + ':' + t.getSeconds();
}

if (window.history.state != null && window.history.state.hasOwnProperty('historic')) {
    if (window.history.state.historic == true) {
        document.body.style.display = 'none';
        console.log('I was here before at ' + formatTime(window.history.state.last_visit));
        window.history.replaceState({historic: false}, '');
        window.location.reload();
    } else {
        console.log('I was forced to reload, but WELCOME BACK!');
        window.history.replaceState({
            historic  : true,
            last_visit: new Date()
        }, '');
    }
} else {
    console.log('This is my first visit to ' + window.location.pathname);
    window.history.replaceState({
        historic  : true,
        last_visit: new Date()
    }, '');
}
好吧,下面是没有注释和赘肉的代码:

window.onunload = function(){};

if (window.history.state != null && window.history.state.hasOwnProperty('historic')) {
    if (window.history.state.historic == true) {
        document.body.style.display = 'none';
        window.history.replaceState({historic: false}, '');
        window.location.reload();
    } else {
        window.history.replaceState({historic  : true}, '');
    }
} else {
    window.history.replaceState({historic  : true}, '');
}
把它贴在你的结束标签之前,你就会有一个相当干净的解决方案

这将适用于被单击的后退/前进的任何组合,并且当用户到达新页面时,不会发生强制重新加载

阅读有关MDN上历史记录对象的更多信息:


经过几天的网上搜索,我终于找到了解决方案,之后添加:

<script>
 window.onbeforeunload = function(){window.location.reload();}
</script>

window.onbeforeunload=函数(){window.location.reload();}
这会很管用的 你会感谢我的


此代码将在您每次访问时重新加载页面。

谢谢cb1。我尝试过,但在更新之前它仍然显示了信息。这是我放在查看页面顶部的内容…+1 Thought这是一个奇怪的@Jerry-它以前每次都对我有效。此外,放入
标题(“Pragma:no cache”)也不会有什么伤害;
在缓存控制标题下-是的,可以将其放在查看页面的顶部。我想您在重新测试之前清除了浏览器的缓存?我确实清除了缓存,并尝试了firefox和chrome。我还添加了第二个代码,但仍然不起作用。谢谢:(我有一个确切的问题,因为Jerry这不适用于“后退”按钮-仅适用于该页面的其他导航操作。我在更新页面上有一个“返回主”按钮,但我不能确定用户不会在浏览器中点击“后退”按钮。此外,我的视图页面中有几种项目类型。我不能简单地添加脚本以强制页面刷新数据来自数据库,因为很难知道用户上次选择了什么类型的项目。但是+1。谢谢。不幸的是,这在Firefox(在v16.0.1中测试)和Firefox(v16.0.1)中不起作用。你确定吗?我所有的旧代码仍然有效。我实际上重新检查了。刚刚测试了这个
/*
 * The following is intentional, to force Firefox to run 
 * this JS snippet after a history invoked back/forward navigation.
 */
window.onunload = function(){};    

function formatTime(t) {
    return t.getHours() + ':' + t.getMinutes() + ':' + t.getSeconds();
}

if (window.history.state != null && window.history.state.hasOwnProperty('historic')) {
    if (window.history.state.historic == true) {
        document.body.style.display = 'none';
        console.log('I was here before at ' + formatTime(window.history.state.last_visit));
        window.history.replaceState({historic: false}, '');
        window.location.reload();
    } else {
        console.log('I was forced to reload, but WELCOME BACK!');
        window.history.replaceState({
            historic  : true,
            last_visit: new Date()
        }, '');
    }
} else {
    console.log('This is my first visit to ' + window.location.pathname);
    window.history.replaceState({
        historic  : true,
        last_visit: new Date()
    }, '');
}
window.onunload = function(){};

if (window.history.state != null && window.history.state.hasOwnProperty('historic')) {
    if (window.history.state.historic == true) {
        document.body.style.display = 'none';
        window.history.replaceState({historic: false}, '');
        window.location.reload();
    } else {
        window.history.replaceState({historic  : true}, '');
    }
} else {
    window.history.replaceState({historic  : true}, '');
}
<script>
 window.onbeforeunload = function(){window.location.reload();}
</script>