【发布时间】:2020-05-07 02:20:58
【问题描述】:
根据我上面的问题,我点击注销按钮重定向到初始页面index.php。
在index.php,当我按下浏览器的返回按钮时,它会显示消息:
"未定义索引:login_user in C:\inetpub\wwwroot\ebooking\pages\dashboard\admin\dashboard_admin.php 在第 6 行"
当我再次按下返回按钮时,它会重定向回 index.php。以下是我的代码:
index.php
<?php
include("config/configPDO.php");
session_start();
$msg = "";
if(isset($_POST['submitBtnLogin'])) {
$User_ID = trim($_POST['Email']);
$email=explode('@',$User_ID);
if (is_array($email)){
$User_ID=$email[0];
}
$Pwd = trim($_POST['Pwd']);
if($User_ID != "" && $Pwd != "") {
$ldap_dn = "TOPPOP\\".$User_ID;
$ldap_password = $Pwd;
$ldap_con = ldap_connect("ldap://xxx.xx.xx.xx:xxx");
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)){;
try {
$records = $conn->prepare("SELECT Email, Role_ID, Pwd FROM Staff WHERE User_ID = :User_ID ");
$records->execute(
array(
'User_ID' => $User_ID,
)
);
$results = $records->fetch(PDO::FETCH_ASSOC);
$message = '';
if($results && count($results) > 0 ){
$_SESSION['login_user'] = $results["Email"];
if($results["Role_ID"] == "2"){
header("location: pages/dashboard/admin/dashboard_admin.php");
}else if ($results["Role_ID"] == "3"){
header("location: pages/dashboard/super_admin/dashboard_super_admin.php");
}else if ($results["Role_ID"] == "1"){
header("location: pages/dashboard/normal_user/dashboard_normal_user.php");
}
} else {
echo "
<script>alert('You're not authorized to use this system')</script>
<script>window.location = 'index.php'</script>
";
}
} catch (PDOException $e) {
echo "Error : ".$e->getMessage();
}
} else{
echo "
<script>alert('Invalid Email or Password')</script>
<script>window.location = 'index.php'</script>
";
}
} else {
$msg = "Both fields are required!";
}
}
?>
dashboard_admin.php(包含注销)
<?php
require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";
$Email = $_SESSION['login_user']; //line 6
?>
check.php
<?php
session_start();
if(isset($_SESSION['login_user']) === false){
header("Location: logout.php");
}
?>
logout.php
<?php
session_start();
session_destroy();
header("Location: ../index.php");
?>
【问题讨论】:
-
重读php.net/manual/en/function.session-destroy.php。我认为您不需要删除会话;只需设置
$_SESSION = array (); -
你不能假设
$_SESSION['login_user']总是被初始化。您必须使用isset()来避免此类错误代码。