我正在处理登录功能......
现在我遇到了问题......就像这样
1)用户按下注销后..
2)如果用户点击浏览器后退按钮..用户可以看到他/她之前登录的页面...
如何限制..用户在注销后返回...
除此之外一切正常......帮我解决这个问题..
这是LoginViewController.php
<?php
session_start();
header("Cache-Control: private, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: Fri, 4 Jun 2010 12:00:00 GMT");
include('GenericClasses/GenericCollectionClass.php');
include('Models/UsersModel.php');
include('DataObjects/Users.php');
include('DatabaseAccess/DBHandler.php');
if(!empty($_SESSION['user']))
{
header("Location:loggedin.php");
die();
}
else
{
?>
//Html code for LoginIndexpage
<?php
}
?>
这是Login.php
<?php
session_start();
header("Cache-Control: private, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: Fri, 4 Jun 2010 12:00:00 GMT");
include('GenericClasses/GenericCollectionClass.php');
include('Models/UsersModel.php');
include('DataObjects/Users.php');
include('DatabaseAccess/DBHandler.php');
if(!isset($_SESSION['user']))
{
header('Location: LoginViewController.php');
exit();
}
echo '<div style="background:white; text-align:right"> Login as:'.$_SESSION['user'].'
<a href="LogoutViewController.php" style="text-align:right">Logout</a></div>';
?>
这是Log-out.php
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
session_start();
session_destroy();
header("Location: LoginViewController.php");
?>
任何建议都是可以接受的......
答案 0 :(得分:1)
用户登录后,您需要在会话变量中存储用户ID或邮件ID。
并且您必须在所有页面上设置条件,如果设置了用户ID或邮件ID的会话变量,则用户可以访问该页面,否则您必须在登录页面上重定向。
答案 1 :(得分:0)
我建议检查每个需要身份验证的页面中的$_SESSION['user']
。这可以使用一个普通的Php页面来实现,您可以在需要身份验证的所有页面中include
(除了登录页面之外可能都是这样)。例如,假设您创建一个名为Security.php
的Php页面,在该页面中,您将从您的问题中提取以下内容:
if(!isset($_SESSION['user']))
{
header('Location: LoginViewController.php');
exit();
}
然后,在每个需要用户身份验证的页面中,您可以在代码的开头包含Security.php
文件。像这样:
<?php
include('YOUR PATH/Security.php');
//Rest of your code here
...
<?
这样,用户点击后退按钮或者如果他通过浏览器的URL输入请求页面无关紧要,如果他没有登录,他将被重定向到登录页面。 / p>
希望这会对你有所帮助
答案 2 :(得分:0)
用户登录后,您必须在内页(用户的仪表板)中设置$_SESSION['id']
之类的会话,并且必须在每个页面上设置条件,即:
if(!isset($_SESSION['user_id'])){
header("location:login.php")
}
这将检查session.Remeber的可用性,以便在logout.php