2013-09-10 36 views
0

首先要做的事情。我知道bcrypt是一個更好的選擇,我確實有一個已經加密的代碼只是godaddy不支持bcrypt。所以我現在必須堅持這個版本。更新數據庫中的密碼

所以我想要一個表單更新/更改用戶的密碼。當然,它不會更新。

的代碼被分成部分的形式:

<?php 
      if(empty($_POST) === false) { 

       if(empty($_POST['current_password']) || empty($_POST['password']) || empty($_POST['password_again'])){ 

        $errors[] = 'All fields are required'; 

       }else if($bcrypt->verify($_POST['current_password'], $user['password']) === true) { 

        if (trim($_POST['password']) != trim($_POST['password_again'])) { 
         $errors[] = 'Your new passwords do not match'; 
        } else if (strlen($_POST['password']) < 6) { 
         $errors[] = 'Your password must be at least 6 characters'; 
        } else if (strlen($_POST['password']) >18){ 
         $errors[] = 'Your password cannot be more than 18 characters long'; 
        } 

       } else { 
        $errors[] = 'Your current password is incorrect'; 
       } 
      } 

      if (isset($_GET['success']) === true && empty ($_GET['success']) === true) { 
       echo '<p>Your password has been changed!</p>'; 
      } else {?> 
      <h1>Change Password</h1> 
      <fieldset> 
      <legend>Log In</legend> 
       <?php 
       if (empty($_POST) === false && empty($errors) === true) { 
        $users->change_password($user['id'], $_POST['password']); 
        header('Location: change-password.php?success'); 
       } else if (empty ($errors) === false) { 

        echo '<p>' . implode('</p><p>', $errors) . '</p>'; 

       } 
       ?> 
       <form action="" method="post"> 
        <table border="0"> 
        <tr> 
        <td width="200"> 
        Current password: 
        </td> 
        <td> 
        <input type="password" name="current_password"> 
        </td> 
        </tr> 
        <tr> 
        <td> 
        New password: 
        </td> 
        <td> 
        <input type="password" name="password"> 
        </td> 
        </tr> 
        <tr> 
        <td> 
        New password again: 
        </td> 
        <td> 
        <input type="password" name="password_again"> 
        </tr> 
        </table> 
        <br> 
        <input type="submit" value="Change password"> 

       </form> 
      <?php 
      } 
      ?> 
      </fieldset> 

和PHP代碼:

public function change_password($user_id, $password) { 

     //global $bcrypt; 

     /* Two create a Hash you do */ 
     $timeNew  = time(); 
     $email_codeNew = hash("sha256", $username + microtime()); 
     $password_hash = hash("sha256", $password); 

     $query = $this->db->prepare("UPDATE `users` SET `password` = ?, `email_code` = ?, `time` = ? WHERE `id` = ?"); 

     $query->bindValue(1, $password_hash); 
     $query->bindValue(2, $email_codeNew); 
     $query->bindValue(3, $timeNew); 
     $query->bindValue(4, $user_id);    

     try{ 
      $query->execute(); 
      return true; 
     } catch(PDOException $e){ 
      die($e->getMessage()); 
     } 

    } 
+2

取而代之的是什麼?有瀏覽器輸出嗎?你有沒有檢查過,以確保你認爲存在的一切都存在? – DiMono

+1

您已確定數據庫中的預期行未更新,但代碼執行了哪些操作?是否有錯誤訊息?你的日誌中有什麼?當你調試它時,它在什麼時候失敗?什麼時候發生相關的運行時間值? – David

+0

瀏覽器輸出爲空,而不是錯誤或成功。它顯示空白。 – patgarci

回答

0

我已經嘲笑了一個PHP網頁,其中做到這一點 - 它也已經過測試和工程我想這可能是因爲你已經使用===很多你的函數,我不確定這些函數的返回,因爲它們不在這裏,但希望如果你運行下面的代碼,你可以添加和調整你已經進入的代碼

編輯用於測試當前密碼總是隻有密碼和更改密碼功能將始終通過。

<?php 

/** 
* @author - Sephedo 
* @for - patgarci @ Stackoverflow 
* @question - http://stackoverflow.com/questions/18728434/updating-the-password-in-the-database 
*/ 

function verifyPassword($password) 
{ 
    return ($password == 'password')? true : false; 
} 

function changePassword($password) 
{ 
    return true; 
} 

if(! empty($_POST)) 
{ 
    // check if all of the fields exists and are not empty 
    if(empty($_POST['current_password']) or empty($_POST['password_new']) or empty($_POST['password_again'])) 
    { 
     $errors[] = "All fields are required"; 
    } 
    elseif(verifyPassword($_POST['current_password'])) // check if the password is valid. 
    { 
     $_POST['password_new'] = trim($_POST['password_new']); 
     $_POST['password_again'] = trim($_POST['password_again']); 

     // check for matching password 
     if($_POST['password_new'] != $_POST['password_again']) $errors[] = "Your passwords do not match"; 

     // check for min limit 
     if(strlen($_POST['password_new']) < 6) $errors[] = "Your new password needs to be at least 6 characters"; 

     // check for min limit 
     if(strlen($_POST['password_new']) > 18) $errors[] = "Your new password needs to be at less than 18 characters"; 

     // Make sure no errors have occured and change password returns true 
     if(! isset($errors) and ! changePassword($_POST['password_new'])) 
     { 
      $errors[] = "An unknown error has occured, please try again"; 
     } 
    } 
    else 
    { 
     $errors[] = "Your current password is invalid"; 
    } 

    // DISPLAY ERRORS 
    if(! isset($errors)) 
    { 
     echo "<span>Your password has been changed!</span>"; 
    } 
    else 
    { 
     foreach((array) $errors as $error) 
     { 
      echo "<span>$error</span><br />"; 
     } 
    } 
} 

?> 

<form method="POST" > 
<fieldset> 
<legend>Change Password</legend> 
<label for="current_password">Current Password</label> <input type="password" name="current_password" /> 
<label for="password_new">New Password</label> <input type="password" name="password_new" /> 
<label for="password_again">Current Password</label> <input type="password" name="password_again" /> 
<input type="submit" value="Save Changes" /> 
</fieldset> 
</form>