【问题标题】:PH: Error occur when press browser back button after destroy sessionPH:销毁会话后按浏览器返回按钮时发生错误
【发布时间】: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 session


【解决方案1】:

第 6 行 dashboard_admin.php 中的未定义索引。该错误是由于试图获取超全局数组$_SESSION 的未定义索引而引起的——它不存在。下一个操作使您能够检查:如果索引 login_user isset in $_SESSION => 使用来自 $_SESSION['login_user'] 的值,否则使用 ''

$Email = $_SESSION['login_user'] ?? '';   //line 6

$Email = isset($_SESSION['login_user']) ? $_SESSION['login_user'] : '';   //line 6

【讨论】:

    【解决方案2】:

    首先检查是否在会话中设置了 login_user,请参见下面的代码

    isset($_SESSION['login_user']) // will return boolean
    

    【讨论】:

    • 对,但在这种情况下这不是完整的解决方案。
    • 然后在您的仪表板 php 中,您只需先检查 login_user 是否仍设置为 else 重定向到。
    • 你完全正确!
    猜你喜欢
    • 2020-04-19
    • 2013-07-20
    • 2013-06-02
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多