返回用户id和Ajax成功响应

返回用户id和Ajax成功响应,ajax,Ajax,我有一个Ajax登录提交,效果很好。现在我需要在成功时将$user\u id发送回登录页面。但我不知道怎么做 下面是我所拥有的 这是php页面 <? if (!securePage($_SERVER['PHP_SELF'])){die();} //Prevent the user visiting the logged in page if he/she is already logged in

我有一个Ajax登录提交,效果很好。现在我需要在成功时将$user\u id发送回登录页面。但我不知道怎么做

下面是我所拥有的

这是php页面

            <?
            if (!securePage($_SERVER['PHP_SELF'])){die();}

            //Prevent the user visiting the logged in page if he/she is already logged in
            if(isUserLoggedIn()) { header("Location: account.php"); die(); }

            //Forms posted
            if(!empty($_POST))
            {
                $errors = array();
                $username = sanitize(trim($_POST["user"]));
                $password = trim($_POST["password"]);

                //Perform some validation
                //Feel free to edit / change as required
                if($username == "")
                {
                    $response['success'] = false;
                }
                if($password == "")
                {
                    $response['success'] = false;
                }

                if(count($errors) == 0)
                {
                    //A security note here, never tell the user which credential was incorrect
                    if(!usernameExists($username))
                    {
                        $response['success'] = false;
                    }
                    else
                    {
                        $userdetails = fetchUserDetails($username);
                        //See if the user's account is activated
                        if($userdetails["active"]==0)
                        {
                            $response['success'] = false;
                        }
                        else
                        {
                            //Hash the password and use the salt from the database to compare the password.
                            $entered_pass = generateHash($password,$userdetails["password"]);

                            if($entered_pass != $userdetails["password"])
                            {
                                //Again, we know the password is at fault here, but lets not give away the combination incase of someone bruteforcing
                                $response['success'] = false;
                            }
                            else
                            {
                                //Passwords match! we're good to go'

                                $response['success'] = true;
                            }

                        }
                    }
                }
            }
            //$user_id = $loggedInUser->user_id;
            echo json_encode($response);
            ?>

此时$response值为true或false,您可以返回一个数组:

$response = array("Success" => true, "UserId" => $user_id);
在您的AJAX响应中,响应变量

response.UserId

将包含用户id

现在看起来很简单,但以前是不可能的。再次感谢你把我踢向正确的方向。
response.UserId