PHP,取决于用户角色的不同索引页

时间:2018-10-26 07:39:49

标签: php mysql ajax redirect login

我已经在mysql数据库中创建了两个用户角色,标准用户和外部用户。如果标准用户登录,则默认情况下他们将访问index.php。我想做的是,当外部用户登录时,他访问另一个页面(external.php)。我在每个页面中都相应地放入了$ userrole =“ External(Standard User)。 下面是login.js文件:

$(document).ready(function () {
    "use strict";
    $("#submit").click(function () {

        var username = $("#myusername").val(), password = $("#mypassword").val();
        var remember;

        if ($("#remember").is(":checked")){
            remember = 1;
        } else {
            remember = 0;
        }

        if ((username === "") || (password === "")) {
          $("#message").fadeOut(0, function (){
            $(this).html("<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Please enter a username and a password</div>").fadeIn();
        });

        } else {
            $.ajax({
                type: "POST",
                url: "ajax/checklogin.php",
                data: {"myusername": username, "mypassword": password, "remember": remember },
                dataType: 'JSON',
                success: function (html) {

                    if (html.response === 'true') {
                       location.reload();
                        return html.username;
                    } else {
                        $("#message").fadeOut(0, function (){
                            $(this).html(html.response).fadeIn();
                        })
                    }
                },
                error: function (textStatus, errorThrown) {
                    console.log(textStatus);
                    console.log(errorThrown);
                    $("#message").fadeOut(0, function (){
                        $(this).html("<div class='alert alert-danger'>" + textStatus.responseText + "</div>").fadeIn();
                    })
                },
                beforeSend: function () {
                    $("#message").fadeOut(0, function (){
                        $(this).html("<p class='text-center'><img src='images/ajax-loader.gif'></p>").fadeIn();
                    })
                }
            });
        }
        return false;
    });
});

这是ajax文件checklogin.php

    <?php

    ob_start();
    require '../../vendor/autoload.php';

    try {
        // Define $myusername and $mypassword
        $username = $_POST['myusername'];
        $password = $_POST['mypassword'];

        // To protect MySQL injection
    $username = stripslashes($username);
    $password = stripslashes($password);

    $response = '';
    $loginCtl = new PHPLogin\LoginHandler;
    $lastAttempt = $loginCtl->checkAttempts($username);
    $max_attempts = PHPLogin\AppConfig::pullSetting("max_attempts", "unsigned");

    //First Attempt
    if ($lastAttempt['lastlogin'] == '') {
        $lastlogin = 'never';
        $loginCtl->insertAttempt($username);
        $response = $loginCtl->checkLogin($username, $password);
    } elseif ($lastAttempt['attempts'] >= $max_attempts) {

      //Exceeded max attempts
        $loginCtl->updateAttempts($username);
        $response = $loginCtl->checkLogin($username, $password);
    } else {
        $response = $loginCtl->checkLogin($username, $password, $_POST['remember']);
    };

    if ($lastAttempt['attempts'] < $max_attempts && $response != 'true') {
        $loginCtl->updateAttempts($username);
        $jsonResp = json_encode(['username'=>$username, 'response'=>$response]);
        echo $jsonResp;
    } else {
        $jsonResp = json_encode(['username'=>$username, 'response'=>$response]);
        echo $jsonResp;
    }

    unset($resp, $jsonResp);
    ob_end_flush();
} catch (Exception $e) {
    error_log($e->getMessage());
    echo json_encode($e->getMessage());
}

0 个答案:

没有答案