php - 我想知道为什么我的登录在 000Webhost 上有效,但在我的系统上尝试时却不起作用

标签 php mysql validation authentication session

当我通过本地主机文件成功注册时,它会成功发送到数据库并回显成功。但是当我尝试使用相同的详细信息登录时,它返回错误。

但是,这与托管网站的情况不同。它在 000webhost 上完美运行。

我的登录代码:

 <?php

if (isset($_POST['login-submit'])) {
    require 'dbh.inc.php';

    $mailusername = $_POST['useremail'];
    $password = $_POST['userpwd'];

    if (empty($mailusername)  || empty($password)) {
        header("Location: ../login.php?error=emptyfields");
        exit();
    }
    else{

        $sql = "SELECT * FROM `users` WHERE username=? OR email=?";

        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../login.php?error=sqlError");
            exit();
        }
        else{

            mysqli_stmt_bind_param($stmt, "ss", $mailusername, $mailusername);
            mysqli_stmt_execute($stmt);

            $result = mysqli_stmt_get_result($stmt);

            if ($row = mysqli_fetch_assoc($result)) {
                $pwdCheck = password_verify($password, $row['password']);

                if ($pwdCheck == false) {
                    header("Location: ../login.php?error=wrongPwd");
                    exit();
                }
                else if($pwdCheck == true){
                    session_start();

                    $_SESSION['userId'] = $row['id'];
                    $_SESSION['username'] = $row['username'];

                    header("Location: ../index.php?login=success");
                    exit();

                }
                else{
                    header("Location: ../login.php?error=wrongPass");
                    exit();
                }
            }
            else{
                header("Location: ../login.php?error=noUser");
                exit();
            }
        }
    }

}
else{
    header("Location: ../login.php");
    exit();
}


我的注册码:

<?php

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

    require 'dbh.inc.php';

    $fullname = $_POST['fullname'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passwordRepeat = $_POST['confirm_password'];

//if empty input
    if (empty($fullname) || empty($username) || empty($email) || empty($password) || empty($passwordRepeat)) {
        header("Location: ../signup.php?error=emptyfields&fullname="); //. $fullname."&username=". $username."&email".$email);
        exit();
    }
    else if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username) ){
        header("Location: ../signup.php?error=invalidemail&username=");
        exit();
    }
    //if email is invalid return error
    elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        header("Location: ../signup.php?error=invalidemail&fullname=");
        exit();
    }
    //checking for valid password
    elseif(!preg_match("/^[a-zA-Z0-9]*$/", $username)){
        header("Location: ../signup.php?error=invalidusername&email=");
        exit();
    }
    //check confirmed password
    else if($password !== $passwordRepeat){
        header("Location: ../signup.php?error=passwordRepeat&username=");
        exit();
    }
    //if username is already taken
    else{

        $sql = "SELECT username FROM `users` WHERE username=?";
        $stmt = mysqli_stmt_init($conn);

        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../signup.php?error=sqlerror");
            exit();
        }
        else{
            mysqli_stmt_bind_param($stmt, "s", $username);
            mysqli_stmt_execute($stmt);

            //did we get a match?

            mysqli_stmt_store_result($stmt);
            $resultCheck = mysqli_stmt_num_rows($stmt);
            if ($resultCheck > 0) {
                header("Location: ../signup.php?error=usernametaken");// .$username);
                exit();
            }
            else{
                $sql = "INSERT INTO `users`(`fullname`, `username`, `email`, `password`) VALUES (?, ?, ?, ?)";

                $stmt = mysqli_stmt_init($conn);
                if (!mysqli_stmt_prepare($stmt, $sql)) {
                    header("Location: ../signup.php?error=sqlerror");
                    exit();
                }
                else{
                    $hashedPwd = password_hash($password, PASSWORD_DEFAULT);


                    mysqli_stmt_bind_param($stmt, "ssss", $fullname, $username, $email, $hashedPwd);
                    mysqli_stmt_execute($stmt);
                    mysqli_stmt_store_result($stmt);
                    header("Location: ../signup.php?signup=success");
                    exit();
                }
            }
        }
    }

    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}
else{
    header("Location: ../signup.php");
    exit();
}

我需要帮助,谢谢。因为我需要集成仪表板,但似乎无法首先登录。

最佳答案

保存由 password_hash() 创建的密码哈希值的列必须至少有 60 个字符才能正常工作。请调整保存哈希值的列的大小,然后重新注册用户。完成后,密码将被正确验证。

来自docs :

PASSWORD_DEFAULT - Use the bcrypt algorithm (default as of PHP 5.5.0). Note that this constant is designed to change over time as new and stronger algorithms are added to PHP. For that reason, the length of the result from using this identifier can change over time. Therefore, it is recommended to store the result in a database column that can expand beyond 60 characters (255 characters would be a good choice).

关于php - 我想知道为什么我的登录在 000Webhost 上有效,但在我的系统上尝试时却不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58321891/

相关文章:

javascript - 弹出警报框 - 点击重定向 "OK"

php - 如何使用 mysql 搜索子级别类别?

mysql - gitlab-ci docker 执行器 |如何设置mysql服务?

php - 如何连接两个具有不同字符串的大表

perl - Perl 是否有适用于所有国家/地区的电话号码验证器?

validation - 如何执行地址验证?

php - Laravel 表单数组验证

php - 在 CodeIgniter 中分组 WHERE 子句

php - mysql 查询错误可以由服务器/托管引起吗?

php - symfony2 JMSSerializerBundle 反序列化具有 OneToMany 关联的实体