2012-08-03 58 views
-1

我读了关于crypt()函数的php.net手册。这里是我的代码:PHP crypt()错误

#code..... 
#we retrieve existing salt and password from database 
$salt=$saltquery['salt']; 
#$ex_password - existing password 
$ex_password=$saltquery['pass']; 
#$pass defined earlier. that's input 
$password_hash=crypt($pass, '$2a$07$'.$salt.''); 
if (crypt($password_hash, $ex_password)==$ex_password) { 
#everything is ok 
} else { 
#username/password combination doesn't exists 
$msgText = "Oops! Check your username and password"; 
$pass=NULL; 
} 

我仍然收到错误'哎呀!检查你的用户名和密码'。我检查数据库和从$password_hash输出,他们匹配。 也许是更好的像这样的代码:

#..... 
if ($password_hash==$ex_password){} 
#..... 

回答

2

您必须将用户输入校验密码时传递给crypt函数(见crypt docs):

if (crypt($user_input, $hashed_password) == $hashed_password) { 
    echo "Password verified!"; 
} 

你是目前正在计算一个新的(不同的)密码散列,并将其与存储的密码进行比较。

+0

我很抱歉这么奇怪的缺陷 – treng 2012-08-03 17:41:13

+0

所以我不需要在数据库中存储盐? – treng 2012-08-03 17:51:16

+1

不分开。它已经存储为密码摘要的一部分(美元符号之间的最后一部分) – knittl 2012-08-03 17:53:09

2

为什么你encryting $ password_hash两次?

我想你比较会更喜欢这样的:

$password_hash=crypt($pass, '$2a$07$'.$salt); 
$password_hash_check($ex_password, '$2a$07$' . $salt); 
if ($password_hash === $password_hash_check) { 
#everything is ok 
} else { 
#username/password combination doesn't exists 
$msgText = "Oops! Check your username and password"; 
$pass=NULL; 
} 
+0

你的代码示例是某种缺陷。也许缺少'= crypt'?即使这样,它不太可能工作(看文档) – knittl 2012-08-03 17:29:30

1

我相信这是你想要什么:

$password_hash=crypt($pass, '$2a$07$'.$salt.''); // crypt input 
if ($password_hash== $ex_password) // check against crypted 
            // password stored in the database