我想使用Php和MySQL更新数据库中特定列的所有行,
例如。:在“名称”列中有一个包含“ MyName”的行,
这是我正在尝试实现的计划和逻辑:
循环数据库
获取每行的当前值,并用hash('...',value);
获取每行的现有值并对其进行哈希处理并进行更新,这就是我要更新完整数据库的方式,
我该如何实现?
首先,我必须说,如果您在数据库中有非敏感数据,则内置的mysql函数可以直接使用mysql的update语句直接为您提供哈希结果。
这个答案并非如此。它与敏感数据有关,例如密码。
我给了您一个PHPpassword_hash()
和password_verify()
示例的链接。
这又是那个链接。左侧的链接用于PDO。以下链接在这里与mysqli类似。
在PDO链接中查看行
$hp=password_hash($ctPassword,PASSWORD_DEFAULT); // hashed password, using
因此,假设您现在有一列名为cleartext的列ctPassword
。您将为alter table
添加新列hashedPassword
。按照提供的链接我,相应调整,哈希值ctPassword
到hashedPassword
一个更新语句。
然后彻底测试。当世界一切正常时,请放下该ctPassword
列,再也不要使用它。为了清楚起见,切勿在数据库中存储明文密码。存储单向哈希值,然后对它们进行验证。上面的链接显示了如何。
这里完全来自PHP,我认为这是需要驱动的,而不是mysql哈希函数yuck。毕竟,您正在使用PHP,并且它们强大的哈希和验证功能将在这里大放异彩。在我看来,最佳实践是mysql的最佳实践,而mysql的人们并没有完全花精力在上面。我全力以赴在mysql中做得尽可能多。但是永远不要使用散列这个主题。让PHP驱动这一程序。
create table sometable
( id int auto_increment primary key,
userName varchar(40) not null,
ctPassword varchar(40) not null -- clear text password (means humans can read it, basically)
-- note, not a great definition of ct but it implies it has not been hashed for safety
);
insert sometable(userName,ctPassword) values
('Brenda','I watch TV too much'),
('Drew','PatriotsWorldChamps'),
('stealth_guy','JFIDU&JF_Anchovies');
这个概念随之而来,嘿,我现在要安全哈希。我可能会被黑。
-- http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
alter table sometable add column hashedPassword varchar(255);
-- now I have 4 columns, hashedPassword is currently nullable
show create table sometable; -- confirms this fact
PHP循环并更新一个新列,以清理之前没有散列的概念(我认为我们在堆栈上都看到了100万次)
用于修补的PHP:
<?php
// turn on error reporting, or wonder why nothing is happening at times
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//mysqli_report(MYSQLI_REPORT_ALL);
error_reporting(E_ALL);
ini_set("display_errors", 1); // Begin Vault
// credentials from a secure Vault, not hard-coded
$servername="localhost";
$dbname="login_system";
$username="dbUserName";
$password="dbPassword";
// End Vault
try {
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $db->prepare("select id,ctPassword from sometable");
$stmt->execute();
$stmt->bindColumn('id', $theId); // bind the results into vars by col names
$stmt->bindColumn('ctPassword', $cPassword); // ditto
// http://php.net/manual/en/pdostatement.fetch.php
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
// as we loop thru here, the $theId and $cPassword variables will be auto-magically updated
// for us because they have been bound as seen above
$hPassword=password_hash($cPassword,PASSWORD_DEFAULT); // we now have a hashed password based on orig clear text one
echo $cPassword . " " . $hPassword . "<br>";
// each time you run this with same data the hashes will be different due to changes in the salt
// based on above PASSWORD_DEFAULT (look at manual page for password_hash)
$sqlUpdate="UPDATE sometable set `hashedPassword`='$hPassword' where `id`=$theId";
$db->query($sqlUpdate);
}
// .. other cleanup as necessary
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit();
}
?>
运行php脚本,验证结果。这些是我的,您的会有所不同。如果再次运行,您甚至会与您的有所不同。代码中提到的原因。
select * from sometable;
+----+-------------+---------------------+--------------------------------------------------------------+
| id | userName | ctPassword | hashedPassword |
+----+-------------+---------------------+--------------------------------------------------------------+
| 1 | Brenda | I watch TV too much | $2y$10$pJ5maui2OlrIPAtISf4u2OqeqEXU9ycDpCNNpp6xDh1uzIv/6ybuW |
| 2 | Drew | PatriotsWorldChamps | $2y$10$kHAKRSeHLi9cghPKTKox/.kXiFgq6ELWwExGcVvbf1yYprtTvi.Ba |
| 3 | stealth_guy | JFIDU&JF_Anchovies | $2y$10$HOkBAkP7ZVIZ7NQB50aKAuhG5WjLHU9AtJCiY2E6h/M2YZuxc2l5K |
+----+-------------+---------------------+--------------------------------------------------------------+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句