【发布时间】:2019-03-28 21:06:40
【问题描述】:
我正在为学校的比赛创建一个项目。我已经正确完成了注册部分。当涉及到登录部分时,我无法正确完成。我尝试在许多其他问题上查找问题网站,但找不到它。即使我在函数中输入密码,函数也会返回 false。
我尝试使用 password_BCRYPT 进行散列,我从 $_POST['password'] 获得的密码,然后使用 hash_equals() 与数据库中的密码进行比较,但它不起作用
这是 login.php 编辑:我忘了添加 register.php 和 file_db.php.Sorry!
<?php
require "file_db.php";
$email= $mysqli->escape_string($_POST['email']);
echo $_POST['email'];
$result= $mysqli->query("SELECT *FROM users WHERE email='$email'");
if($result->num_rows == 0) {
$SESSION['message']="Nu exista niciun utilizator cu acel email";
header("location:error.php");
} else {
$user=$result->fetch_assoc();
$hash=substr($user['password'],0,60);
if(password_verify($_POST['password'],$hash)) {
$_SESSION['email']=$user['email'];
$_SESSION['firstname']=$user['firstname'];
$_SESSION['lastname']=$user['lastname'];
$_SESSION['active']=$user['active'];
$_SESSION['loggedin']=true;
echo $_SESSION['loggedin'];
echo 1;
} else{
$_SESSION['message']="Ai introdus o parola gresita!";
}
}
?>
<?php
require "file_db.php";
$firstname = $mysqli->escape_string($_POST['firstname']);
$lastname = $mysqli->escape_string($_POST['lastname']);
$email = $mysqli->escape_string($_POST['email']);
$password =$mysqli->escape_string(password_hash($_POST['password'],PASSWORD_BCRYPT));
$hash = $mysqli->escape_string(md5(rand(0,1000) ) );
$_SESSION['email']=$_POST['email'];
$_SESSION['firstname']=$_POST['firstname'];
$_SESSION['lastname']=$_POST['lastname'];
$result = $mysqli->query("SELECT * FROM users WHERE email ='$email' ") or die($mysqli->error());
if($result->num_rows > 0)
{
$_SESSION['message']='Exista un utilizator cu acest email deja!';
header("location:error.php");
}
else{
$sql="INSERT INTO users (firstname,lastname,email,password,hash)".
"VALUES('$firstname','$lastname','$email','$password','$hash')";
if( $mysqli->query($sql) )
{
$_SESSION['active']=0;
$_SESSION['message']="Link de confirmare a fost trimis la $email, te rugam sa iti verifici contul accesand link-ul trimis in email!";
$to = $email;
$subject='Account Verification';
$messageb='
Salut'.$firstname.',
Multumim pentru ca te-ai inscris!
Apasa pe acest link pentru a-ti activa contul:
https://localhost/aWEBDEVFII/verify.php?email='.$email.'&hash'.$hash;
mail($to, $subject, $messageb);
header("location:success.php");
}
else{
$_SESSION['message']='Inregistrarea a intampinat o eroare!';
header("location: error.php");
}
}
?>
``````
<?php
session_start();
$host='localhost';
$user='root';
$pass='';
$db='filesdb';
$mysqli= new mysqli($host,$user,$pass,$db) or die($mysqli->error);
?>
【问题讨论】:
-
substr($user['password'],0,60);嗯? -
什么是
var_dump($user['password']);? -
mmh,你修改了哈希,想知道为什么验证失败?你为什么要这样做?附加说明:散列和加密是不同的东西。
-
@Qirel var_dump($user['password']);是字符串(60)“$2y$10$TVDLLUcuKEsBDaXPaU7haOWvsTpLbg6aELmCao1PKewxpxadysieu”