0

我正在尝试加密给定的密码以匹配数据库中的密码。但是crypt()每次使用都会给我一个不同的结果,所以它永远不会匹配。我怎样才能使这项工作。

这是对用户给出的密码进行哈希处理的语句。

if (empty($_POST) === false) {
        $username = $_POST['username'];
        $password = crypt($_POST['password']);

在此之前,我手动创建了一个用户,crypt('password')但如果我在该字段中输入“密码”,则它不匹配。

4

3 回答 3

4

试试下面:

if (isset($_POST['username']) && isset($_POST['password'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];

  // get the hashed password from database
  $hashed_password = get_from_db($username);

  if (crypt($password, $hashed_password) == $hashed_password) {
    echo "Password verified!";
  }
}
于 2013-05-02T06:43:22.543 回答
2

像这样试试

//$pass_entered_from_login is the user entered password
//$crypted_pass is the encrypted password from the
//database or file
if(crypt($pass_entered_from_login,$crypted_pass)) == $crypted_pass)
{
   echo("Welcome to my web site.")
}

阅读更多

于 2013-05-02T06:41:40.233 回答
-2

crypt auto generates the salt each time you use it ............ so use the same salt for the user do this while registering the user to your database and while checking tooo.

if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = crypt($_POST['password'],$_POST['username']);
 }

NOTE: the 2nd parameter is the salt in crypt function .

hope it helps :)

于 2013-05-02T06:44:17.603 回答