我尝试在登录脚本中使用ajax,但页面不会重定向


I tried using ajax in login script but the page wont redirect

我的登录脚本有一个问题,我在那里实现了ajax,所以如果登录无效,消息会显示在同一页面上,但当用户输入正确的信息时,我有一些问题。什么都没有发生,我必须刷新页面,然后它会说欢迎用户,但是当用户输入错误的信息时会立即显示无效的登录消息。

index.php

<?php
if (!isset($_SESSION['uid'])) {
    echo "<p>Welcome to our homepage. you can be part of our awesome community by signing up.</p>";
}else{
    echo "<p>You are now logged in and able to post comments in our site</p>";
}
?>
<?php
if (!isset($_SESSION['uid'])) {
    echo "<form action='login_parse.php' method='post' style='display: inline-block'>
    Username: <input type='text' name='username' id='username' />
    Password: <input type='password' name='password' id='password' />
    <input id='submit-button' type = 'button' value='login' id='submit' />";
    echo "</form>";
    echo "<form action='signup.php' method='post' style='display: inline-block'>";
    echo "&nbsp";
    echo "<input type='submit' name='submit' value='Signup'>";
    echo "</form>";
} else {
    echo "<form style='display: inline-block'>";
    echo "<p>You are logged in as ".$_SESSION['username']."";
    echo "</form>";
    echo "<form action='logout_parse.php' method='post' style='display: inline-block'>";
    echo "&nbsp";
    echo "<input type='submit' value='logout'>";
    echo "</form>";
    echo "<form action='profile.php' method='post' style='display: inline-block'>";
    echo "&nbsp";
    echo "<input type='submit' value='profile'>";
    echo "</form>";
}
?>
<script src="ajax.js"></script>
<script>

var HTTP = loadXMLDoc();
var submitEvent = document.getElementById("submit-button").onclick = function(){
    hello2(HTTP);
};
</script>

ajax.js

function hello2(){
    var username = encodeURIComponent(document.getElementById('username').value);
    var password = encodeURIComponent(document.getElementById('password').value);
    var url = "login.php?username="+username+"&password="+password;
    HTTP.onreadystatechange=function()
    {
        if (HTTP.readyState==4 && HTTP.status==200)
        {
            document.getElementById("ack1").innerHTML=HTTP.responseText;
        }
    };
    HTTP.open("POST", url ,true);
    HTTP.send();
}

login.php

<?php
session_start();
include_once("connect.php");
if (isset($_REQUEST['username'])) {
    $username = mysql_real_escape_string( $_GET["username"]);
    $password = mysql_real_escape_string( md5 ($_GET["password"]));
    $sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1";
    $res = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($res) == 1) {
        $row = mysql_fetch_assoc($res);
        $_SESSION['uid'] = $row['id'];
        $_SESSION['username'] = $row['username'];
            header("location: index.php");
        exit();
    } else{
        echo "INVALID login information.";
        exit();
    }
}
?>

为了解决这个问题,我尝试使用login.php中的头函数刷新页面,但仍然一无所获,我不得不手动刷新页面,然后加载欢迎用户。我做错什么了吗。

使用ajax调用文件时,不能使用header/服务器端重定向。

相反,您必须在ajax调用的success函数中检查结果,并在登录处理正确的情况下使用javascript重定向。

以下是我所做的一些更改,因此它现在可以正常工作

   function hello2(){
    var username = encodeURIComponent(document.getElementById('username').value);
    var password = encodeURIComponent(document.getElementById('password').value);
    var url = "login.php?username="+username+"&password="+password;
HTTP.onreadystatechange=function()
  {
    if (HTTP.readyState==4 && HTTP.status==200)
    {
        if (HTTP.responseText == 1){
                    window.location.replace("index.php");
        }
        else{
                document.getElementById("ack1").innerHTML=HTTP.responseText;
        }
   } 
  } 
HTTP.open("POST", url ,true);
HTTP.send();

}

您可以将location.reload()放入HTTPhandler中。

HTTP.onreadystatechange=function()
    {
        if (HTTP.readyState==4 && HTTP.status==200)
        {
            document.getElementById("ack1").innerHTML=HTTP.responseText;
        }
        else {
            location.reload();
        }
    };

当用户输入正确的凭据时,您不会为ajax调用返回正确的响应。

if(mysql_num_rows($res) == 1) {
        $row = mysql_fetch_assoc($res);
        $_SESSION['uid'] = $row['id'];
        $_SESSION['username'] = $row['username'];
           echo "Logged in as ".$row['username'];
        exit();
    } else{
        echo "INVALID login information.";
        exit();
    }

您可以使用JavaScript的window.location.replace('redirectURL')重定向到特定页面。