第一件事。我知道 bcrypt 是一个更好的选择,而且我确实有一个具有 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());
}
}