Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to log in and then redirect to a custom page, based on user type. My problem is that when I'm pressing the login button, on the console I get that that I have a Undefined array key pageType, and I don't understand why... If I'm for example on teacher page, I set $_SESSION['pageType'] = 'teacher'; This my login.html page:
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>

This is checklogin.js:
JavaScript
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)
            }
        });
} 

And this is my redirect.php file:
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>";
    }

?>

What I suspect is that the redirect() function it's not even loaded, so that's why I think I'm getting this error... Sometimes I'm seeing ,,XHR failed loading: GET" I modified a code in order to have another login.html page, this is my old login.html code:
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>


What I have tried:

I've tried everything but it doesn't want to work
Posted
Updated 24-Jan-21 21:29pm
v2
Comments
Richard MacCutchan 24-Jan-21 9:22am    
Please put <pre> tags around the code blocks, so they are clear and readable, and indicate where the problem occurs.
Richard Deeming 25-Jan-21 3:31am    
var info = "?enterID=" + enterID + "&password=" + password;
$.ajax({
    type: "GET",
    url: "php/login.php" + info,

Aside from the fact that you haven't encoded the URL parameters properly, you've also just stored the user's credentials in the browser's history, exposing them to anyone else who can see the computer, and any casual network sniffer.

NEVER pass the credentials in the URL. Make a POST request instead, and pass the credentials in the body of the request. And make sure your site is only served over HTTPS.
Richard Deeming 25-Jan-21 3:34am    
Other than that, you haven't shown any code which sets the pageType session variable. Your redirect code doesn't check that this variable has been set; it just tries to access it blindly.

The error message you're getting suggests that this variable has not been set. You need to debut the code that's supposed to set it to see what the problem is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900