在jQuery中访问PHP $ _SESSION变量并执行错误处理和页面重定向

时间:2014-10-25 00:28:05

标签: javascript php jquery session redirect

我正在尝试使用jQuery访问PHP会话变量,以便我可以使用jQuery而不是PHP重定向用户。现在,当用户登录时,会运行一个PHP脚本,用于检查数据库的登录和注册状态。如果用户已经登录,则使用PHP将其重定向到拒绝页面,如果未注册,则重定向到注册页面。如果登录尝试成功,则会将用户重定向到其个人资料页面。我现在的工作原理是,当用户登录时会出现冗余。

PHP

    ob_start();
    require("../includes/header.php");
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $p_num = $_POST["username"];
        $pwd = $_POST["password"];
        ob_clean();

        $query = "SELECT * FROM `$user_table` WHERE `user_id` = '$p_num'";
        $result = mysqli_query($connect, $query) or die(mysqli_error($connect));

        while($row = mysqli_fetch_assoc($result)){
            $user_id = "{$row['user_id']}";
            $first_name = "{$row['first_name']}";
            $last_name = "{$row['last_name']}";
            $user_name = $first_name ." " .$last_name;
            $password = "{$row['password']}";
            $image = "{$row['image']}";
            $email = "{$row['email']}";
            $program = "{$row['program']}";
            $role = "{$row['role']}";
            $status = "{$row['logged_in']}";
            $registered = "{$row['registered']}";

            if(($user_id == $p_num) && password_verify($pwd, $password)){
                $_SESSION["id"] = $user_id;
                $_SESSION["user"] = $user_name;
                $_SESSION["program"] = $program;
                $_SESSION["pass"] = $password;
                $_SESSION["image"] = $image;
                $_SESSION["email"] = $email;
                $_SESSION["role"] = $role;
                $_SESSION["status"] = $status;
                $_SESSION["registered"] = $registered;
                $loggedin = "UPDATE `$user_table` SET `logged_in` = 1 WHERE `user_id` = '$user_id'";

                if($_SESSION["status"] == 1){
                    header("Location: ../pages/denied_login.php");
                }
                elseif($_SESSION["registered"] == 0){
                    header("Location: ../pages/not_registered.php");
                }
                else{
                    header("Location: ../pages/instructor.php");
                    mysqli_query($connect, $loggedin);
                }
            }
            else{
                header("Refresh: 1; URL=../index.php");
            }
        }
        echo json_encode(array("success"=>1));
    }

的jQuery

$("#login").on("click", function(){
        $.post("../php/login.php", {}, function(response){
            if(response.success == "1"){
                location.replace("../pages/instructor.php");
            }
        })
    })

冗余带有javascript的location.replace函数和PHP的头函数。脚本以它应该运行的方式运行。所以我的问题是两部分。有没有办法在jQuery中进行错误检查和重定向?和。我应该简单地使用jQuery onclick处理程序调用登录脚本,让PHP处理所有错误并重定向吗?

1 个答案:

答案 0 :(得分:1)

您无法从客户端访问$ _SESSION。您需要做的是从PHP输出“success”值和一个可选变量,如果“success”不是1,则表示重定向目标。

PHP:

// lots of code
if($_SESSION["status"] == 1){
    echo json_encode(array('success' => 0, 'redirect' => '/pages/denied_login.php'));
}

jQuery的:

$("#login").on("click", function(){
    $.post("../php/login.php", {}, function(response){
        if(response.success == "1"){
            location.replace("../pages/instructor.php");
        } else if(response.success == "0") {
            location = response.redirect;
        }
    })
})