首页 > 解决方案 > 登录时未定义的数组键 pageType,即使它已添加到页面

问题描述

我正在尝试登录,然后根据用户类型重定向到自定义页面。我的问题是,当我按下登录按钮时,在控制台上我发现我有一个未定义的数组键 pageType,但我不明白为什么......如果我在教师页面上,我设置 $_SESSION['pageType'] = 'teacher'; 这是我的 login.html 页面:

<img class="wave" src="img/wave.png">
    <div class="container_main" onsubmit="redirect();">
        <div class="img">
            <img src="img/bg.svg">
        </div>
        <div class="login-content">
            <form onsubmit="redirect();"> 
                
                <img src="img/avatar.svg">
                <h2 class="title">Quiz Website</h2>
                <div class="input-div one">
                   <div class="i">
                        <i class="fas fa-user"></i>
                   </div>
                   <div class="div">
                        <input type="text" class="input" id="enterID" placeholder="Username">
                   </div>
                </div>
                <div class="input-div pass">
                   <div class="i"> 
                        <i class="fas fa-lock"></i>
                   </div>
                   <div class="div form-group">
                        <input id="password" type="password" class="form-control" placeholder="Password" value="" required/>
                   </div>

                </div>
                <a href="page/registration.php">Don't have an account?</a>
                <div class="form-group">
                        <a href="page/forget-password.php" class="ForgetPwd">Trouble logging in?</a>
                    </div>
                <input type="submit" class="btn" value="Login" onclick="login();" readonly >
                <div class="error-message" id="errorMessage"></div>
            </form>

        </div>
    </div>

</html>

这是 checklogin.js:

function login() {
    var enterID = document.getElementById("enterID").value;
    var password = document.getElementById("password").value;
    if ((password != "") && (enterID != "")) {
        var info = "?enterID=" + enterID + "&password=" + password;
        $.ajax
            ({
                type: "GET",
                async: true,
                url: "php/login.php" + info,
                success: function (respond) {
                    if (respond == "admin") {
                        window.location.href = "page/admin-system-management.php";
                    } else if (respond == "student") {
                        window.location.href = "page/student-dashboard.php";
                    } else if (respond == "teacher") {
                        window.location.href = "page/teacher-dashboard.php";
                    } else {
                        document.getElementById("errorMessage").innerText = respond;
                    }


                }
            });
    }
    else {
        document.getElementById("errorMessage").innerText = "Please fill in all the fields.";
    }
}

function redirect() {
    $.ajax
        ({
            type: "GET",
            async: true,
            url: "php/redirect.php",
            success: function (respond) {

                if (respond != "not logged.") {
                    if (respond == "admin") {
                        window.location.href = "page/admin-system-management.php";
                    } else if (respond == "student") {
                        window.location.href = "page/student-dashboard.php";
                    } else if (respond == "teacher") {
                        window.location.href = "page/teacher-dashboard.php";
                    }
                }
                console.log(respond)
            }
        });
} 

这是我的 redirect.php 文件:

<?php
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

    if (isset($_SESSION["type"])){
        if ($_SESSION["type"] != $_SESSION["pageType"]){
            $alert_message = "You cannot access this page using your account!";
            $link = "../login.html";
            echo "<script type='text/javascript'>alert('$alert_message'); window.setTimeout(function(){ if ('".$_SESSION["type"]."'=='admin'){ window.location.href = '../page/admin-system-management.php';} else if ('".$_SESSION["type"]."'=='student'){ window.location.href = '../page/student-dashboard.php';} else if ('".$_SESSION["type"]."'=='teacher'){ window.location.href = '../page/teacher-dashboard.php';}  }, 0);</script>";
        }

    } else {
        $alert_message = "Your login period has expired! Please login again!";
        $link = "../login.html";
        echo "<script type='text/javascript'>alert('$alert_message'); window.setTimeout(function(){ window.location.href = '$link'; }, 0);</script>";
    }

?>

我怀疑它甚至没有加载 redirect() 函数,所以这就是为什么我认为我收到此错误...有时我看到 ,,XHR failed loading: GET" 我修改了一个代码以便拥有另一个 login.html 页面,这是我的旧 login.html 代码:

<body class="jumbotron vertical-center" onload="redirect();">
    <div class="container login-container">
        <div class="row">
            <div class="col-md-4 login-form mx-auto">
                <h3>Login</h3>
                <h6>Don't have an account?<a href="page/registration.php" class=""> Register</a></h6>
                <form>
                    <div class="error-message" id="errorMessage"></div>
                    <div class="form-group">
                        <input id="enterID" type="text" class="form-control" placeholder="Your User ID *" value="" required/>
                    </div>
                    <div class="form-group">
                        <input id="password" type="password" class="form-control" placeholder="Your Password *" value="" required/>
                    </div>
                    <div class="form-group">
                        <input class="btn btn-block btn-primary" value="Login" onclick="login();" readonly />
                    </div>
                    <div class="form-group">
                        <a href="page/forget-password.php" class="ForgetPwd">Trouble logging in?</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>

标签: javascriptphphtmlajax

解决方案


好的,这是我认为您需要的更新。

重定向.php

<?php

if(!session_id()) {
    session_start();
}

// you can change this line to this once you have debugged your code - $data = [];
$data = ['session' => $_SESSION];

// place code here to get the user type from the database
// then use $_SESSION['type'] = '{database response}';

if(!isset($_SESSION['type']))
{
    $_SESSION['type'] = null;
}

switch($_SESSION['type']) {
    case 'admin':
        header('Location: /page/admin-system-management.php'); // this is the php way to redirect
        break;

    case 'student':
        header('Location: /page/student-dashboard.php');
        break;

    case 'teacher':
        header('Location: /page/teacher-dashboard.php');
        break;

    default:
        $data['alert_message'] = 'Your login period has expired! Please login again!';
        
}
echo json_encode($data);
exit;

?>

Javascript重定向功能

    function redirect() {
        $.ajax
        ({
            type: "GET",
            async: true,
            url: "php/redirect.php",
            success: function (response) {

                console.log(response);

                if(response.alert_message) {
                    alert(response.alert_message);
                }
            }
        });
    }

推荐阅读