<?php
// you should put your db connection stuff here
require('connect.php');
//you create a new column to store hashed passwords. Good idea if
//something goes bad. You should drop the column with the original
// passwords once every thing is ok and done.
$result = mysqli_query(
$conn,
'alter table users add column hashed_password varchar(255) not null'
);
if ($result===FALSE)
{
// handle error here
}
$result = mysqli_query($conn, 'select * from users');
if ($result===FALSE)
{
// handle error here
}else
{
while($user = mysqli_fetch_assoc($result)
{
// you could use PASSWORD_DEFAULT here but I wouldn't. If in a
// future migration the default password crypt function changes
// your system won't work and it will be hard to know why.
$hashedPassword = password_hash($user['password'], PASSWORD_BCRYPT);
$result2 = mysqli_query($conn,'update users set hashed_password = \''. mysqli_real_escape_string($hashedPassword) .'\' where id=\''. $user['id'] .'\'');
if ($result2 === FALSE)
{
//handle error here
}
}
}
然后您只需检查hashed_password列中的密码而不是原始密码。如果一切正常并且您可以登录没有问题,您可以删除原始密码列,您就完成了。