登录表单无效的用户名/密码

时间:2018-09-16 14:47:58

标签: php sql database

我有一个登录表单,但是即使我输入了正确的电子邮件和密码,我仍然会得到无效的用户名/密码。我正在查看我的代码并尝试调试它,但似乎找不到问题。

  • 表格

    <form method="post" action = 'plogin.php'>
        <h2 class="sr-only">Login Form</h2>
        <div class="illustration"><i class="icon ion-ios-navigate"></i></div>
        <div class="form-group"><input class="form-control" type="email" name="email" placeholder="Е-маил"></div>
        <div class="form-group"><input class="form-control" type="password" name="password" placeholder="Лозинка"></div>
        <div class="form-group"><button class="btn btn-primary btn-block" type="submit">Најава</button></div><a href="#" class="forgot">Ја заборавивте лозинката? Кликнете овде.</a></form>
    

  • 表单验证
     

    if(!empty($_POST)) {
        include('includes/general.php');
        if(!$connection){
            die("Failed to connect to database ".mysqli_connect_error());
        }
        $email = mysqli_real_escape_string($connection, $_POST['email']);
        $password = mysqli_real_escape_string($connection, $_POST['password']);
        $hashedPassword = hash('SHA256', $password);
        $query = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
        $result = mysqli_query($connection, $query) or die (mysqli_error($connection));
        if(mysqli_num_rows($result) > 0) {
            $_SESSION = mysqli_fetch_array($result);
            header('location: user.php');
        } else {
            echo "<script>alert('invalid username/password');
                window.location.href= 'login.php';</script>";
        }
    } else {
        header('location: index.php');
    

1 个答案:

答案 0 :(得分:0)

您对密码进行哈希处理,但不要使用哈希...

if(!empty($_POST)) {
    include('includes/general.php');
    if(!$connection){
        die("Failed to connect to database ".mysqli_connect_error());
    }
    $email = mysqli_real_escape_string($connection, $_POST['email']);
    $password = mysqli_real_escape_string($connection, $_POST['password']);
    $hashedPassword = hash('SHA256', $password);
    $query = "SELECT * FROM users WHERE email = '$email' AND password = '$hashedPassword'";
    $result = mysqli_query($connection, $query) or die (mysqli_error($connection));
    if(mysqli_num_rows($result) > 0) {
        $_SESSION = mysqli_fetch_array($result);
        header('location: user.php');
    } else {
        echo "<script>alert('invalid username/password');
            window.location.href= 'login.php';</script>";
    }
} else {
    header('location: index.php');

也许尝试上面的代码