symfony2浏览器的后退按钮

时间:2015-11-18 11:47:30

标签: php symfony browser header logout

我的问题:当用户“注销”我的网站并按下浏览器中的后退按钮时,她仍然可以查看之前的页面。

我尝试了这个食谱Symfony2 response - Clear cache headers on back button但没有任何反应

配方:

$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);

我的 security.yml 在防火墙中有这些设置

logout:
    path:   mypath_logout
    target: /
    invalidate_session: true

注销链接时收到的标题:

object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#429 (5) { ["computedCacheControl":protected]=> array(5) { ["max-age"]=> string(1) "0" ["must-revalidate"]=> bool(true) ["no-cache"]=> bool(true) ["no-store"]=> bool(true) ["private"]=> bool(true) } ["cookies":protected]=> array(0) { } ["headerNames":protected]=> array(2) { ["cache-control"]=> string(13) "Cache-Control" ["date"]=> string(4) "Date" } ["headers":protected]=> array(2) { ["cache-control"]=> array(1) { [0]=> string(55) "max-age=0, must-revalidate, no-cache, no-store, private" } ["date"]=> array(1) { [0]=> string(29) "Wed, 18 Nov 2015 11:40:47 GMT" } } ["cacheControl":protected]=> array(4) { ["max-age"]=> string(1) "0" ["must-revalidate"]=> bool(true) ["no-cache"]=> bool(true) ["no-store"]=> bool(true) } }

我在用户注销时使用render,这样:

$response = $this->render('template.html.twig', array(
        'form' => $form->createView(),
    ));

此外,以防万一我在使用普通PHP销毁会话时注销:

unset($_SESSION);
session_destroy();

相当烦人的问题,这个“防止后退按钮”,花了很多时间在它上面:(

2 个答案:

答案 0 :(得分:0)

这里有一些js,你可以在用户在注销时被重定向到的页面上包含,确定这不适用于非js案件等但我认为这是我找到的最佳解决方案,当我花时间找出

(function ($, global) {

    var _hash = "!",
    noBackPlease = function () {
        global.location.href += "#";

        setTimeout(function () {
            global.location.href += "!";
        }, 50);
    };

    global.setInterval(function () {
        if (global.location.hash != _hash) {
            global.location.hash = _hash;
        }
    }, 100);

    global.onload = function () {
        noBackPlease();

        // disables backspace on page except on input fields and textarea.
        $(document.body).keydown(function (e) {
            var elm = e.target.nodeName.toLowerCase();
            if (e.which == 8 && elm !== 'input' && elm  !== 'textarea') {
                e.preventDefault();
            }
            // stopping event bubbling up the DOM tree..
            e.stopPropagation();
        });
    }

})(jQuery, window);

答案 1 :(得分:0)

试试这些代码

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.

你可以在这里查看:

How to control web page caching, across all browsers?