2014-01-13 42 views
1

我想使用php重置用戶密碼。我從html表單中獲得了用戶的當前密碼和新密碼。這裏的PHP腳本來重置密碼。但即使用戶輸入正確的密碼,它也始終執行else部分。如何?任何解決方案?我知道可能有一個簡單的錯誤,但我是新來的,無法找到任何錯誤。在php中使用密碼加密的基本錯誤

$uid = $_SESSION['uid']; 
    $current_pass = $_POST['org_pass']; 
    $new_pass = $_POST['new_pass']; 

    if(isset($_POST['submit'])) 
    { 
      $act_pass = $db_con->prepare("SELECT password FROM user WHERE u_id= ?"); 
      $act_pass->bindParam(1,$uid); 

      $act_pass->execute(); 

      $actual_pass = $act_pass->fetchColumn(); 

      define('SALT', 'flyingrabbit'); 

      $typed_pass = md5(SALT.$actual_pass); 

      if ($typed_pass == $current_pass) 
      { 
       $new_pass1 = md5(SALT . $new_pass); 

       $res = $db_con->prepare("UPDATE user SET password= ? WHERE u_id=?"); 
       $res->bindParam(1,$new_pass1); 
       $res->bindParam(2,$uid); 

       $res->execute(); 

       header("Location: profile.php"); 
       exit; 
      } 
      else 
      { 


        echo "<script type=\"text/javascript\">window.alert(\"You entered wrong password.\");window.location.href = 'profile.php';</script>"; 

      } 

    } 
+0

你的意思是用$ actual_pass更換$ current_pass? – BetaBlaze

+0

我想用'$ new_pass'替換'$ actual_pass'。 '$ current_pass' ..用戶以html格式輸入,這是他的密碼。 '$ actual_pass'是存儲在數據庫中的用戶密碼。 – HungryDB

+2

您指定:if($ typed_pa​​ss == $ current_pass) 您是否100%肯定這是您想要的?在我看來,你應該這樣做: if($ typed_pa​​ss == $ actual_pass) 對於typed_pa​​ss,您將數據庫中的密碼轉換爲哈希值。你應該這樣做,而不是數據庫中的內容。 – BetaBlaze

回答

2

這看起來錯:

$actual_pass = $act_pass->fetchColumn(); 

// ... 

$typed_pass = md5(SALT.$actual_pass); 

if ($typed_pass == $current_pass) 

您將從數據庫中獲取的信息(我假設已經哈希)進行哈希處理。

你可能想:

$actual_pass = $act_pass->fetchColumn(); 

// ... 

$typed_pass = md5(SALT.$current_pass); 

if ($typed_pass == $actual_pass) 

注意md5 is not recommended哈希密碼。

+0

ohh..right。謝謝。而關於MD5。我搜索谷歌並閱讀1或2本關於加密的書籍,他們推薦md5。怎麼樣? – HungryDB

+0

@HungryDB查看我答案底部的鏈接。 – jeroen

1

,因爲你比較$typed_pass == $current_pass但前行,你這樣做$typed_pass = md5(SALT.$actual_pass)你比較散列它轉到else語句,鹽漬密碼爲明文密碼

2

你應該比較散列$ current_pass和** $ actual_pas ** s。

替換

typed_pa​​ss

$ = MD5(SALT $ actual_pass。);$ typed_pa​​ss = md5(SALT。$ current_pass); $ typed_pa​​ss == $ current_pass$ typed_pa​​ss == $ actual_pass