2013-11-20 23 views
1

以下教程:http://www.gregboggs.com/php-blowfish-random-salted-passwords/我一直在尝试在我的注册表单中对密码实施加密。目前我运行的代码没有任何错误,但没有数据被添加到数据库。我知道SQL语句是正确的,因为它在我开始实现加密功能之前正在工作。密码加密有问题,没有错误?

这是我到目前为止有:

<?php 

    CRYPT_BLOWFISH or die ('No Blowfish found.'); 

    include_once "config.php"; 
    include_once "lib\password.php"; 

    //This string tells crypt to use blowfish for 5 rounds. 
    $Blowfish_Pre = '$2a$05$'; 
    $Blowfish_End = '$'; 

    if($_POST["username"] && $_POST["email"] && $_POST["password1"] && $_POST["password2"]) { 

     if($_POST["password1"] = $_POST["password2"]) { 

      $password1 = mysql_real_escape_string ($_POST["password1"]); 

      // Blowfish accepts these characters for salts. 
      $Allowed_Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./'; 
      $Chars_Len = 63; 

      // 18 would be secure as well. 
      $Salt_Length = 21; 
      $salt = ""; 

      //$mysql_date = date('Y-m-d');    

      for($i=0; $i<$Salt_Length; $i++) 
      { 
       $salt .= $Allowed_Chars[mt_rand(0,$Chars_Len)]; 
      } 
      $bcrypt_salt = $Blowfish_Pre . $salt . $Blowfish_End; 

      $hashed_password = crypt($password1, $bcrypt_salt); 

      /* create a prepared statement */ 
      $stmt = mysqli_prepare($link, "INSERT INTO `users` (`username`, `email`, `password`, `salt`) VALUES (?, ?, ?, ?)"); 

      /* bind parameters for markers */ 
      mysqli_stmt_bind_param($stmt, "ssss", $_POST["username"], $_POST["email"], $hashed_password, $salt); 

      /* execute query */ 
      mysqli_stmt_execute($stmt); 

      /* close statement */ 
      mysqli_stmt_close($stmt); 

      print "<h1>You have registered sucessfully!</h1>"; 

      print "<a href='main_login.html'>Log in</a>"; 

     } 
     else print "Your passwords do not match, try again!"; 
    } 
    else print "Please fill out the entire form!"; 

/* close connection */ 
mysqli_close($link); 

?> 

PHP版本注: 作为WAMP服务器只目前支持php5.4.12本身,我使用这个兼容库:https://github.com/ircmaxell/password_compat

BUMP:我已经过了一段时间了,我仍然无法找到数据未插入的原因。我再次测试了SQL语句。我回应了$ password1,$ bcrypt_salt,$ hashed_pa​​ssword整个代码,以确保它们正常工作,并且这些变量都包含正确的信息。有任何想法吗?

+0

'if($ _ POST [“password1”] = $ _POST [“password2”])'不应该这样'if($ _ POST [“password1”] === $ _POST [“password2”])'? 第一个将永远为真 – peaceman

+0

嗨,我知道这一点。这一定是我的疏忽,谢谢,因为发现了这个! – Corey

+1

您是否在语句准备之前检查了$ bcrypt_salt和$ hashed_pa​​ssword的值?那些'mysqli_ *'函数的结果值是什么?似乎只是一个基本的调试任务。 – peaceman

回答

1

如果你只是想比较$密码1和$密码2,那么你只需要检查:

$hash1 = password_hash("$password1", PASSWORD_BCRYPT, $options)."\n"; 
$hash2 = password_hash("$password2", PASSWORD_BCRYPT, $options)."\n"; 
if ($hash1 == $hash2) { 
    echo 'matched'; 
} 

但是,这不会是非常有用的,你想从用户密码1 $,并从$ HASH2 商店,然后:

if (password_verify($password1, $hash2)) { 
    echo 'matched'; 
}