简体   繁体   English

强制页面从服务器重新加载而不是加载缓存版本

[英]Force page to reload from server instead of load the cached version

I have webpage A. The user clicks on a form to submit data, and it takes him to webpage B. When he clicks the back button, I need webpage A to be refreshed from the server, rather that loaded from cache. 我有网页A.用户点击表单提交数据,然后将他带到网页B.当他点击后退按钮时,我需要从服务器刷新网页A,而不是从缓存加载。 I have this: <meta http-equiv="expires" content="0"> but it doesnt seem to work. 我有这个: <meta http-equiv="expires" content="0">但它似乎没有用。 I have also tried setting a variable on page B (session variable via php) and then check for it on page A and refresh (or not) according to its existence. 我也尝试在页面B上设置变量(通过php设置会话变量)然后在页面A上检查它并根据它的存在刷新(或不刷新)。 This doest seem to work either. 这种做法似乎也有效。 The basic code for that is: Page A: 基本代码是:页面A:

<?php 
if(isset($_SESSION['reloadPage'])) {
unset($_SESSION['reloadPage']);
echo'
<script>
window.location.replace("/****/****/*****.php");
</script>
';
}
?>

And on page B: 在第B页:

$_SESSION['reloadPage'] = 1;

With the PHP solution, it simply keeps trying to refresh the page in a never ending loop. 使用PHP解决方案,它只是不断尝试在永无止境的循环中刷新页面。 Something in my logic missing? 我的逻辑缺少什么? Is this the right way to go about it? 这是正确的方法吗?

EDIT Upon further investigation, when you tell the browser to not cache the page, does that force a full server side refresh as well? 编辑经过进一步调查,当您告诉浏览器不缓存页面时,这是否会强制完整的服务器端刷新? That's what I need. 这就是我需要的。 A full server side refresh of the page. 页面的完整服务器端刷新。

OK try this instead: 好吧试试这个:

<?php
    if(!session_id()) {
        @session_start();   
    }
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    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'); 

    if(isset($_SESSION['form_submitted'])) {
        unset($_SESSION['form_submitted']);
        header('Location: ?' . uniqid());
        #header('Refresh: 0');
    }
?>

You will need to set $_SESSION['form_submitted'] = true on page 2. 您需要在第2页上设置$ _SESSION ['form_submitted'] = true。

Try with all three: 试试这三个:

<meta http-equiv="cache-control" content="no-cache"> <!-- tells browser not to cache -->
<meta http-equiv="expires" content="0"> <!-- says that the cache expires 'now' -->
<meta http-equiv="pragma" content="no-cache"> <!-- says not to use cached stuff, if there is any -->
<?php 
    session_start();
    if(isset($_SESSION['reloadPage'])) {
        unset($_SESSION['reloadPage']);
           //no outputting code above header
        header("Cache-Control: no-cache, must-revalidate");
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
        header("Location: http://www.mypage.com/");
    }
?>

I know this is old question and already have the answer (that not refreshing from the server). 我知道这是一个老问题,已经有了答案(不是从服务器刷新)。 So I would like to share a solution using jQuery. 所以我想用jQuery分享一个解决方案。

$('#back-button').click(function(){
   setTimeout(location.reload(true), 1000);
});

Explanation: 说明:
As you click the back button, the setTimeout will triggered after 1 second (1000 milliseconds). 单击后退按钮时, setTimeout将在1秒(1000毫秒)后触发。 Then, the page will reload from the server as the parameter inside the reload is true . 然后,当重新加载内的参数为true ,页面将从服务器reload

If you put true inside the reload it will reload from the server while if you put false it will reload from the cache. 如果你在reload true ,它将从服务器重新加载,而如果你输入false ,它将从缓存重新加载。 Source w3schools . 来源w3schools

Your JavaScript should be... 你的JavaScript应该......

window.location = "/****/****/*****.php"

I think that should fix your loop. 我认为应该修复你的循环。

<?php 
  session_start();
  if(isset($_SESSION['reloadPage'])) {
  unset($_SESSION['reloadPage']);
  echo'
  <script>
  window.location.replace("/****/****/*****.php");
  </script>
  ';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM