gpt4 book ai didi

PHP password_hash(), password_verify()

转载 作者:可可西里 更新时间:2023-10-31 23:06:09 31 4
gpt4 key购买 nike

我的注册脚本接受用户密码,然后使用 PHP 的 password_hash 函数对密码进行加密,然后将其放入数据库中。当我使用刚创建的用户登录时,出现检查密码是否相同的错误。就我而言,他们不是。我在登录脚本中调用 password_verify 函数时做错了什么?

注册

if($_SERVER["REQUEST_METHOD"] == "POST"){
function secure($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return($data);
}

$p_num = secure($_POST["p_number"]);
$first_name = secure($_POST["first_name"]);
$last_name = secure($_POST["last_name"]);
$email = secure($_POST["email"]);
$password = secure($_POST["pw"]);
$verify_password = secure($_POST["pw_verify"]);
$program = secure($_POST["program"]);
$role = secure($_POST["role"]);
$logged_in = 0;
$registered = 0;
$image = "../images/profile_placeholder.png";

if($password != $verify_password){
echo "Nope. Passwords";
}
else{
$registered = 1;
$password = password_hash($password, PASSWORD_DEFAULT);
$insert = "INSERT INTO `$user_table`(`user_id`, `first_name`, `last_name`, `password`, `image`, `email`, `program`, `role`, `logged_in`, `registered`) VALUES('" .$p_num ."', '" .$first_name ."', '" .$last_name ."', '" .$password ."', '" .$image ."', '" .$email ."', '" .$program ."', '" .$role ."', '" .$logged_in ."', '" .$registered ."')";
$query = mysqli_query($connect, $insert);
echo "Success!";
}
}

登录

if($_SERVER["REQUEST_METHOD"] == "POST"){
$p_num = $_POST["username"];
$pwd = $_POST["password"];

$query = "SELECT * FROM `$user_table` WHERE `user_id` = '$p_num'";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result)){
$user_id = "{$row['user_id']}";
$first_name = "{$row['first_name']}";
$last_name = "{$row['last_name']}";
$user_name = $first_name ." " .$last_name;
$password = "{$row['password']}";
$image = "{$row['image']}";
$email = "{$row['email']}";
$program = "{$row['program']}";
$role = "{$row['role']}";
$status = "{$row['logged_in']}";
$registered = "{$row['registered']}";
if(($user_id == $p_num) && (password_verify($pwd, $password))){
$_SESSION["id"] = $user_id;
$_SESSION["user"] = $user_name;
$_SESSION["program"] = $program;
$_SESSION["pass"] = $password;
$_SESSION["image"] = $image;
$_SESSION["email"] = $email;
$_SESSION["role"] = $role;
$_SESSION["status"] = $status;
$_SESSION["registered"] = $registered;
$loggedin = "UPDATE `$user_table` SET `logged_in` = 1 WHERE `user_id` = '$user_id'";
}
var_dump($pwd);
var_dump($password);
}

这是我执行 var_dump 时得到的结果:

string(1) "1" string(16) "$2y$10$0aysCso3b"

很明显,密码没有匹配在一起。因此,在注册脚本中,密码被散列并发送到数据库。然后,当用户登录时,登录脚本会查看用户输入的登录密码,然后使用 password_verify 将其与数据库中的散列密码进行核对。然而,散列密码不接受未散列密码作为匹配项。我不明白的是,为什么?

最佳答案

这是我用于 password_hashpassword_verify 的内容。按照编写的方式进行尝试,一旦成功,您就可以开始添加其余代码。

修改表名和列名以适应。

注意:这是一种基本的插入方法。我建议你使用 prepared statements相反。

旁注:密码列需要足够长以容纳散列 VARCHAR(255)。请参阅“脚注”。

插入文件

<?php
$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';

$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}

$password = "rasmuslerdorf";
$first_name = "john";
$password = password_hash($password, PASSWORD_DEFAULT);

$sql = "INSERT INTO users (`name`, `password`) VALUES ('" .$first_name ."', '" .$password ."')";

$query = mysqli_query($conn, $sql);
if($query)

{
echo "Success!";
}

else{
// echo "Error";
die('There was an error running the query [' . $conn->error . ']');
}

登录文件

<?php
// session_start();

$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';

$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}

$pwd = "rasmuslerdorf";
$first_name = "john";

//$sql = "SELECT * FROM users WHERE id = 1";

$sql = "SELECT * FROM users WHERE name='$first_name'";
$result = $conn->query($sql);
if ($result->num_rows === 1) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (password_verify($pwd, $row['password'])) {

//Password matches, so create the session
// $_SESSION['user'] = $row['user_id'];
// header("Location: http://www.example.com/logged_in.php");

echo "Match";

}else{
echo "The username or password do not match";
}

}

mysqli_close($conn);

脚注:

密码列应该足够长以容纳散列。 72 长是散列产生的字符长度,但手册建议 255。

引用:

"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 password_hash(), password_verify(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536293/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com