使用php登录成功后如何重定向到新页面

时间:2019-05-04 03:17:26

标签: php redirect login

感谢您的考虑。 我会简明扼要,因为无论发布什么内容,我似乎都被禁止了。 我在这里发现了类似的问题,但是所有建议都与我的代码无关。

登录成功后,我只需要帮助即可重定向到我的代码所在的新页面。

 $username = $_POST["username"]; 

    $conn = mysqli_connect($host, $user, $pass, $db);


    $query = "SELECT * FROM user WHERE username = '" .$username.  "'";
    $result = mysqli_query($conn,$query);

    while ($row = mysqli_fetch_assoc($result)){
        echo "Password Entered: " . $_POST["password"];
        echo "Correct Pasword: " . $row['password'];

        // See if the password is correct
        if ($_POST["password"] === $row['password'])
            echo "Logon Successful!";


        else {
            echo "Logon Failed!";
        }
    }
     if (!mysqli_fetch_assoc($result))
        echo "Invalid Username";
?>

2 个答案:

答案 0 :(得分:0)

也许会有一些变化,如下所示:

login.php

$username = $_POST["username"]; 
$password = $_POST["password"];
$conn = mysqli_connect($host, $user, $pass, $db);
$query = "SELECT * FROM user WHERE username = '" . $username .  "'";
$result = mysqli_query($conn,$query);

while ($row = mysqli_fetch_assoc($result)){
    // See if the password is correct
    if ($password === $row['password']){
        header('location: login_successful.php');
    }else {
        // you can hide the message at QueryString via SESSION or COOKIE
        header('location: login_form.php?message=FAIL_MESSAGE');  //you can detect and show login status message.
    }
}

答案 1 :(得分:0)

我只是在标题后面忘记了分号。

$query = "SELECT * FROM user WHERE username = '" .$username.  "'";
$result = mysqli_query($conn,$query);

while ($row = mysqli_fetch_assoc($result)){

    // See if the password is correct
    if  ($_POST["password"] === $row['password']){
        echo "Logon Successful!";
        header("Location: index.php"); 
        exit();
    }