在我的管理頁面我有我可以編輯用戶頁面的表單。當我加載編輯頁面和表單時,我也從數據庫中加載信息,因爲如果這些字段是空的,那麼在提交表單後,它們在數據庫中也將變爲空。更新數據庫中的字段
問題在於密碼字段。當表單加載時,密碼字段爲password
,並且顯示爲••••••••••••••••••••••••••••••••••••••••
,它是DB 4d9012b4a77a9524d675dad27c3276ab5705e5e8
的編碼密碼。如果我不改變密碼,也做了場在數據庫更新不輸入相同的密碼,併成爲該8122c907fcf084364519b613b3ba6a3a88c9f980
..這是編輯文件
// keep track post values
$username= $_POST['username'];
$password = sha1($_POST['password']);
$email = $_POST['email'];
$fileName = $_FILES['user_image']['name'];
$tmpName = $_FILES['user_image']['tmp_name'];
$fileSize = $_FILES['user_image']['size'];
$fileType = $_FILES['user_image']['type'];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = date('Y-m-d') . '-' .$fileName;
// save image path
$path = "../../img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
}
}
// update data
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($fileName != null && $fileName != '')
{
$sql = "UPDATE users set username = ?, password = ?, email = ?, user_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($username,$password,$email,$path,$user_id));
}
else
{
$sql = "UPDATE users set username = ?, password = ?, email = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($username,$password,$email,$user_id));
}
if (isset($_POST)) {
$_SESSION['edited'] = '<center><code>Done!</code></center>';
} else {
$_SESSION['edited'] = false;
}
header('Location: users.php');
}
else
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM users where user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($user_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$username = $data['username'];
$password= sha1($data['password']);
$email = $data['email'];
$user_image = $data['user_image'];
Database::disconnect();
}
?>
<form role="form" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
User ID: <b><?php echo $user_id;?></b>
</div>
<!-- Text input-->
<div class="form-group">
<label for="username">Username</label>
<input value="<?php echo !empty($username)?$username:'';?>" id="username" name="username" class="form-control" type="text">
</div>
<!-- File Button -->
<div class="form-group">
<label for="user_image">image</label>
<input id="userl_mage" name="user_image" class="input-file" type="file" value="<?php echo !empty($user_image)?$user_image:'';?>">
</div>
<!-- File Button -->
<div class="form-group">
<label for="password">Password</label>
<input id="password" name="password" class="form-control" type="password" value="<?php echo !empty($password)?$password:'';?>">
</div>
<!-- Text input-->
<div class="form-group">
<label for="email">Email</label>
<input id="email" name="email" class="form-control" type="text" value="<?php echo !empty($email)?$email:'';?>">
</div>
我所看到的可能的解決辦法是顯示實際的密碼所以當表單submited和密碼沒有改變哈希相同的密碼。但我不想看到密碼。
任何想法如何做到這一點?
讓密碼字段爲空。存儲的密碼在任何情況下都應該保持散列。當然,您需要檢查更新時是否設置了密碼字段,是否符合您的應用程序密碼標準。 –
除了密碼相關的問題,我可以推薦這篇文章。它在教程中解釋瞭如何使用blowfish加密來安全地存儲密碼。 http://code.tutsplus.com/tutorials/understanding-hash-functions-and-keeping-passwords-safe-net-17577 – Ben
是的Ben,我明白這一點,但我在@Arcturil解釋回答爲什麼我不想要並且在這個系統中需要「更高級」的加密。感謝您的教程順便說一句。 –