[英]symfony2 browser's back button
我的問題是:當用戶“注銷”我的網站並按瀏覽器中的后退按鈕時,她仍然可以檢查以前的頁面。
我已經嘗試過此食譜Symfony2響應-清除“后退”按鈕上的緩存標題,但是什么也沒有發生
食譜:
$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();
這個“防止后退按鈕”很煩人,花了很多時間:(
這是一些您可以在注銷時將用戶重定向到的頁面上的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);
試試這些代碼
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
您可以在這里檢查:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.