提交表单的浏览器缓存问题

Browser Cache Issue On Submitted Form

我认为这不是框架问题。在我的研究中,这似乎是浏览器缓存问题,但我尝试过的方法都没有用。

不幸的是,尽管我不想让用户点击后退按钮,但显然没有办法(真的)阻止用户点击后退按钮。

这是表单上传的结果。

这很好用,我得到了预期的结果。

当用户点击后退按钮时,他们得到的是原始表格,而不是刚刚上传的图片。

在用户点击提交表单的后退按钮后点击刷新后,他们会得到应该显示的上传图片。

我有两个模板 layout.php 和 layout_wide.php

我在两个模板中都试过了。

    <meta http-equiv="cache-control" content="max-age=0" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="pragma" content="no-cache" />

我试过这个自定义助手...

<?php
function include_stylesheets_versioned()
{
    $response = sfContext::getInstance()->getResponse();
    sfConfig::set('symfony.asset.stylesheets_included', true);
    $html = '';
    foreach ($response->getStylesheets() as $file => $options) {
        if ((strpos($file, '?') === false) && (stripos($file, 'http') !== 0) ) {
            $file .= '?v='.sfConfig::get('app_resource_version');
        }
    $html .= stylesheet_tag($file, $options);
    }
    echo $html;
}
function include_javascripts_versioned()
{
    $response = sfContext::getInstance()->getResponse();
    sfConfig::set('symfony.asset.javascripts_included', true);
    $html = '';
    foreach ($response->getJavascripts() as $file => $options) {
        if ((strpos($file, '?') === false) && (stripos($file, 'http') !== 0) ) {
            $file .= '?v='.sfConfig::get('app_resource_version');
        }

    $html .= javascript_include_tag($file, $options);
    }

    echo $html;
}

我准备尝试将其放入模式或 iframe 中以解决此表单的后退按钮问题。但是我有另一种形式有类似的问题,其中不能选择模态。

我昨天确实看到了 Post Redirect Get Design Pattern 并认为它可能是一个解决方案,但我想知道为什么元标记根本不起作用。

我意识到这可能是一个重复的问题,但我找不到有效的解决方案。

还有其他人 运行 参与其中吗?有人可以指点我解决方案吗?

该应用程序位于 AWS 实例上,服务器是 NGINX(如果有任何影响的话)。

提前谢谢你。

请仔细检查哪个 your browser cache is clean 并遵循此 article

您需要为 HTTP-EQUIV="Expires" 设置 CONTENT="-1" 而不是 0

你还需要根据这个answer

使用<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, no-store, must-revalidate"/>
<HTML>
<HEAD>
<TITLE>---</TITLE>
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, no-store, must-revalidate">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD>
<BODY>

Text in the Browser Window

</BODY>
<HEAD>
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, no-store, must-revalidate">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD>
</HTML>

但是 HTTP-EQUIV is highly unreliable so you need to send HTTP header from NGINX 所以首先创建一个配置文件并将其命名为 expires.conf 然后将其包含在你的原始 nginx confing 中:

server {
    listen ...;
    root ...;
    index ...;

    server_name ...;

    charset ...;
    include expires.conf; #here

    ...
}

在你的 expires.conf 中说你不想缓存静态文件(或任何你不想要的):

location ~* \.(?:manifest|appcache|html?|xml|json)$ {
  expires -1;
}

备注:

  • 注意单词的大小写。例如P拉格玛
  • 在 html 结构底部使用 HEADMETA 标记是 IE 缓存策略的解决方法。

我需要重做布局模板以符合新标准。它们是很久以前原创的。

这有效,并在后退按钮上给我一个新的时间结果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <?php
    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");
 ?>

<?php include_javascripts() ?>
<?php include_stylesheets() ?>
<?php include_http_metas() ?>
<?php include_metas() ?>
<?php include_title() ?>
<link rel="shortcut icon" href="/favicon.ico" />
</head>
<body>
 <?php echo $sf_content ?>
</body>
</html>
<?php echo time(); ?>

我认为最大的变化是 DOCTYPE

旧文档类型

<!DOCTYPE html>

注意****!!!!

以上的

None 在 symfony 1.4.20

中工作

对于仍在使用 1.4 的少数人,我必须将以下内容放入操作中才能使其正常工作。

    $this->getResponse()->setHttpHeader('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0, private'); 
    $this->getResponse()->setHttpHeader('Pragma', 'private'); 
    $this->getResponse()->setHttpHeader('Expires', 0);