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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10132446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:00:28  来源:igfitidea点击:

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

phpjavascriptjquery

提问by mdance

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. I have this: <meta http-equiv="expires" content="0">but it doesnt seem to work. 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. This doest seem to work either. The basic code for that is: Page A:

我有网页A。用户单击表单提交数据,然后将他带到网页B。当他单击后退按钮时,我需要从服务器刷新网页A,而不是从缓存加载。我有这个:<meta http-equiv="expires" content="0">但它似乎不起作用。我还尝试在页面 B 上设置一个变量(通过 php 的会话变量),然后在页面 A 上检查它并根据它的存在刷新(或不刷新)。这似乎也不起作用。其基本代码是: Page 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. Something in my logic missing? Is this the right way to go about it?

使用 PHP 解决方案,它只是不断尝试以永无止境的循环刷新页面。我的逻辑中缺少什么?这是正确的方法吗?

EDITUpon further investigation, when you tell the browser to not cache the page, does that force a full server siderefresh as well? That's what I need. A full server side refresh of the page.

编辑经过进一步调查,当您告诉浏览器不要缓存页面时,这是否也强制完整的服务器端刷新?这就是我需要的。页面的完整服务器端刷新。

采纳答案by Kyle Hudson

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。

回答by Kristian

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 -->

回答by adeneo

<?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/");
    }
?>

回答by Sendoh Akira

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 setTimeoutwill triggered after 1 second (1000 milliseconds). Then, the page will reload from the server as the parameter inside the reloadis true.

说明:
当您单击后退按钮时,setTimeout将在 1 秒(1000 毫秒)后触发。然后,页面将从服务器重新加载作为reloadis 中的参数true

If you put trueinside the reloadit will reload from the server while if you put falseit will reload from the cache. Source w3schools.

如果你把它放在true里面,reload它将从服务器重新加载,而如果你把false它从缓存中重新加载。来源w3schools

回答by ilyes kooli

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

回答by The Man

Your JavaScript should be...

你的 JavaScript 应该是...

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

I think that should fix your loop.

我认为这应该可以解决您的循环。