回声不显示。 PHP

时间:2018-06-15 21:37:41

标签: php

每个标题重定向后都有一个echo。但它没有弹出。因此,当用户输入无效的登录详细信息时,不会弹出任何消息。我究竟做错了什么? 我也尝试了一种JavaScript方法,但没有设法解决问题。 是否与我的嵌套ifs有关?

<?php

session_start();

#first if
if (isset($_POST['submit'])) {

    include 'dbh.inc.php';

    $uid = mysqli_real_escape_string( $conn , $_POST['uid'] );
    $pwd = mysqli_real_escape_string( $conn , $_POST['pwd'] );

    //Error handlers
    //Check if this input are empty
    #second if
    if (empty($uid) || empty($pwd)) {
        header("Location: ../index.php?login=empty");
        exit();
    }/*second else*/ else {
        $sql = "SELECT * FROM users WHERE user_uid='$uid' OR user_email='$uid'";
        $result = mysqli_query($conn,$sql);
        $resultCheck = mysqli_num_rows($result);
        #third if
        if ($resultCheck < 1) {

            header("Location: ../index.php?login=error");
            echo "Login error";
            exit();
        }/*third else*/ else {
            #forth if
            if ($row = mysqli_fetch_assoc($result)) {
                //de-hashing the password
                $hashedPwdCheck = password_verify($pwd , $row['user_pwd']);
                #fifth if
                if ($hashedPwdCheck == false) {

                    header("Location: ../index.php?login=error");
                    echo "Login error";


                    exit();
                } /*fifth else*/ elseif ($hashedPwdCheck == true) {
                    //Log in the user here
                    $_SESSION['u_id'] = $row['user_id'];
                    $_SESSION['u_first'] = $row['user_first'];
                    $_SESSION['u_last'] = $row['user_last'];
                    $_SESSION['u_email'] = $row['user_email'];
                    $_SESSION['u_uid'] = $row['user_uid'];
                    $uid = $_SESSION['u_id'];
                    header("Location: ../index.php?login=success");
                    echo "Login error";
                    exit();
                }
            }
        }
    }
}/*first else*/ else {
    header("Location: ../index.php?login=error");
    echo "Login error";
    exit();
}
?>

1 个答案:

答案 0 :(得分:0)

如果使用location标题,则永远不会显示消息 - 浏览器会忽略请求的其余部分并立即进行重定向,因为HTTP代码更改为302.

即使您可以显示消息,对用户来说也不是一个好的体验,因为它只会显示几分之一秒,然后重定向将发生,页面将被覆盖。您应该在登录页面上显示错误消息(index.php?login = error)。

相关问题