如何从sqlite db读取/检索哈希密码以使用php登录

如何从sqlite db读取/检索哈希密码以使用php登录,php,sqlite,Php,Sqlite,我已成功创建了具有哈希密码的注册表,如下所示。我无法使用存储在SQLite数据库中的用户名和哈希密码登录 如果密码存储为纯文本,我可以登录,但我希望对密码进行哈希处理。请帮助我如何读取与登录用户名匹配的哈希密码。我是php新手,这是我第一次使用SQLite db和php。请帮帮我,过去几天我一直在努力做到这一点: 任何帮助或指导都会很好 我注意到我在网上找不到多少关于php和sqlite的内容,我在stack flow上看到的类似问题大多与mysql有关。此外,我很抱歉,如果我张贴的权利,或者如

我已成功创建了具有哈希密码的注册表,如下所示。我无法使用存储在SQLite数据库中的用户名和哈希密码登录

如果密码存储为纯文本,我可以登录,但我希望对密码进行哈希处理。请帮助我如何读取与登录用户名匹配的哈希密码。我是php新手,这是我第一次使用SQLite db和php。请帮帮我,过去几天我一直在努力做到这一点: 任何帮助或指导都会很好

我注意到我在网上找不到多少关于php和sqlite的内容,我在stack flow上看到的类似问题大多与mysql有关。此外,我很抱歉,如果我张贴的权利,或者如果它是一个太容易的答案,因为我是初学者和新的这一点。多谢各位

下面是我如何将密码存储在我的registration.php中的hash中,它正在工作:

if(ISSET($_POST['register'])) {
//setting the variables
$user_id = $_POST['user_id'];
**//hashing the password
$password = PASSWORD_HASH($_POST['password'], PASSWORD_DEFAULT);**
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
    
//Inserting
$query = "INSERT INTO myTable (user_id, password, email, first_name, last_name) VALUES(:user_id, :password, :email, :first_name, :last_name)";
    $stmt = $db->prepare($query);
    $stmt->bindParam(':user_id', $user_id);
    $stmt->bindParam(':password', $password);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':first_name', $first_name);
    $stmt->bindParam(':last_name', $last_name);
    
//check to see if the exec of the query is success
    if($stmt->execute()){
        $_SESSION['success'] = "Account is created successfully";
            
    //redirect to login
            header("location:index.html");
    }
}
这是我的login.php

您需要使用将db密码与输入的密码进行比较


程序应从myTable中选择密码,其中用户与输入的用户代码匹配。如果返回了任何行,即找到了用户,请使用password_verify比较从数据库返回的内容和用户输入的内容。类似于代码后面的注释位。

如何与登录页面上输入的哈希值进行比较?是否有类似的语法或来源,你可以告诉我,因为我不知道如何比较,这是我试图寻求帮助。答案更正。
    $user_id = $_POST['user_id'];
    $password = ($_POST['password']);
    $query = "SELECT count(*) as count FROM myTable WHERE user_id=:user_id AND password=:password";

    $stmt = $db->prepare($query);
    $stmt->bindParam(':user_id', $user_id);
    $stmt->bindParam(':password', $password);    
    // this is to execute 
    $result = $stmt->execute();
    //this fetching from the db the user name and password. Where SQLite3_NUM returns an array by column number starting at 0 being the first
    $row = $result->fetchArray(SQLITE3_NUM);
    
/*$stmt = $pdo->prepare("SELECT * FROM lit_login_credentials WHERE user_id = ?");
$stmt->execute([$_POST['user_id']]);
$stmt = $stmt->fetch();*/

    //This is checking if the input login and user in the db is only entered once then it will go though else if not found then it will stay on the same page. 
    if($row[0] == 1 /*&& $user_id && password_verify($_POST['password'], $user_id['password'])*/){
        //$out = "Success";
        header('location: admin.php');
    } else {
        //$out = "invalid username or password";
        //echo "<div class='alert alert-danger'>Invalid username or password</div>";
        $_SESSION['error'] = "Invalid username or password";
        header('location: index.php');
    }