在php中实现crypt()

在php中实现crypt(),php,mysql,crypt,Php,Mysql,Crypt,我正在尝试加密给定的密码以匹配数据库中的密码。但是,每次使用crypt()都会得到不同的结果,因此它永远不会匹配。我怎样才能做到这一点 下面是对用户给出的密码进行哈希运算的语句 if (empty($_POST) === false) { $username = $_POST['username']; $password = crypt($_POST['password']); 在此之前,我手动创建了一个拥有crypt('password')的用户,但是如果我在

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

下面是对用户给出的密码进行哈希运算的语句

if (empty($_POST) === false) {
        $username = $_POST['username'];
        $password = crypt($_POST['password']);
在此之前,我手动创建了一个拥有
crypt('password')
的用户,但是如果我在字段中输入了“password”,它就不匹配了。

像这样尝试

//$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.")
}

像这样试试

//$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.")
}

尝试以下操作:

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!";
  }
}
请尝试以下内容:

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!";
  }
}

crypt每次使用时都会自动生成盐。。。。。。。。。。。。 因此,为用户使用相同的盐 在将用户注册到数据库和检查tooo时执行此操作

if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = crypt($_POST['password'],$_POST['username']);
 }
注意:第二个参数是salt-in-crypt函数


希望有帮助:)

每次使用时,crypt都会自动生成盐。。。。。。。。。。。。 因此,为用户使用相同的盐 在将用户注册到数据库和检查tooo时执行此操作

if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = crypt($_POST['password'],$_POST['username']);
 }
注意:第二个参数是salt-in-crypt函数

希望有帮助:)