这个问题已经在这里有了答案: > How to use password_hash 7个
> How do you use bcrypt for hashing passwords in PHP? 9个
我得到了这份任务,直到现在,我一直在使用sha1进行安全保护.
上星期五,老师回到了我们身边,告诉我们使用password_hash.
知道这是明天的事,我试图弄清楚它是如何工作的,但不要把我的头缠住.
我发现有很多人在谈论它,但这些都不对我有用:
How to use password_hash Register And Login
目前,正如分配的那样,我仅使用PDO并返回到以前的工作代码(使用sha1)
<?PHP
ob_start();// TEST
include("inc/timer.inc.PHP");//session
require("inc/database.inc.PHP");//connection website
$title='website';
if (isset($_POST['formConnection'])) {
$loginConnection = filter_input(INPUT_POST,'loginConnection',FILTER_SANITIZE_FULL_SPECIAL_CHARS );
// Connection sha1- OLD
$passwordConnection = sha1($_POST['passwordConnection']);
// Connection password_hash
//$hash = $profile['password'];
//$passwordConnection = password_verify($_POST['passwordConnection'],$hash);
if (!empty(($loginConnection) AND !empty($passwordConnection))) {
$connection = $website->prepare("SELECT * FROM members WHERE login = ? AND password= ?");
$connection->execute(array($loginConnection,$passwordConnection));
$userExists = $connection->rowCount(); //Test existence et affectation à la session des valeurs
if ($userExists == 1) {
$profile = $connection->fetch();
$_SESSION['idMember'] = $profile['idMember'];
$_SESSION['login'] = $profile['login'];
$_SESSION['status'] = $profile['status'];
header("Location: member-detail.PHP?idMember=".$_SESSION['idMember']);
} else {
echo "<script>alert(\"Wrong login or password\")</script>";
}
} else {
echo "<script>alert(\"Please check your login or your password\")</script>";
}
}
?>
<body>
<form method="post" action="">
<div class="form-group">
<label for="loginConnection">login</label><br>
<input type="text" class="form-control" name="loginConnection" id="loginConnection"
placeholder="login" required><br><br>
</div>
<div class="form-group">
<label for="passwordConnection">password</label><br>
<input type="password" class="form-control" name="passwordConnection" id="passwordConnection"
placeholder="Mot de Passe" required><br><br>
</div>
<input type="submit" name="formConnection" value="Se connecter">
<div class="form-group">
<a href="subscribe.PHP">Not subscribed yet?</a>
</div>
</form>
<br><br>
</body>
我知道它应该是布尔值,但我不知道如何使用它.
是否有循序渐进的教程?我可能已经错过了.
谢谢
password_verify()
功能将与password_hash()
功能结合使用.
您将从password_hash()生成的哈希存储在数据库中.当某人尝试登录时,您可以根据哈希测试他们提供的密码.如果password_verify()返回true,则密码匹配.
您不应该使用password_hash()来重新哈希密码,因为you will get a different answer every time(如果使用随机盐,则应这样做).当您使用password_hash()对密码进行哈希处理时,默认情况下,它将使用随机盐对哈希进行哈希处理.该随机盐被编码为结果哈希字符串,以便password_verify()可以使用与原始哈希相同的盐对其进行验证.
基本上,您应该从数据库中检索尝试登录的用户的哈希,并将其提供给password_verify()函数.除了盐之外,哈希还包含有关使用哪个哈希算法的信息.
<?PHP
// See the password_hash() example to see where this came from.
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf',$hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>