繁体   English   中英

用户单击后退按钮时维护登录页面

[英]Maintain login page when user clicks back button

当用户注销并单击浏览器中的“后退”按钮时,该用户可以像登录时一样查看该页面。

我该如何避免呢?

在php中,您可以进行会话。 用户登录时,可以在该会话中设置一个变量,以便知道他们已登录以及以谁登录。 注销后,将完全清除会话。 然后,当您单击“后退”按钮时,页面将加载为没有会话Cookie,并且您将看到正确的行为。

这是一个非常简单的示例,但是希望它可以为您提供一个如何使其适用于您的网站的想法。

的login.php

//Load an existing session, or create a new session
session_start();

//You probably want to check the passwordhash against a stored
//password hash and only set this when the password was correct    
$_SESSION["uid"] = 12345;

test.php(登录时此页面将显示“ a”,否则显示“ b”)

//Load an existing session, or create a new session
session_start();

//If the user is logged in, echo "a", otherwise echo "b"
if( isset( $_SESSION["uid"] ) && $_SESSION["uid"] == 12345 ) {
  echo "a";
} else {
  echo "b";
}

logout.php

//Load an existing session, or create a new session
session_start();

//Remove any information in the session
$_SESSION = array();

//Invalidate the cookie associated with this session (it expires 1 second
//before now)
setcookie( session_name(), '', time() - 1 );

//Finally internally destroy the session. It no longer exists.
session_destroy();

暂无
暂无

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

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