我想使用密碼哈希使用phpmysql。問題是密碼_驗證似乎並沒有爲我工作到目前爲止。假設我的密碼在註冊時是'123456789'。我將它存儲在數據庫中使用密碼哈希不工作在PHP mysql
password_hash('123456789', PASSWORD_BCRYPT, array('cost' => 12));
然後,當我在登錄字段中輸入'123456789'時,它什麼都不做,失敗。
這裏是我的代碼:
<?php
session_start();
include('db.php');
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<p/>
<?php
if(isset($_POST['login']) && $_POST['login'] == 'Login') {
$loginEmail = $_POST['loginEmail'];
$loginPassword = $_POST['loginPassword'];
$sqlLogin = $db->prepare("SELECT * FROM registered_users WHERE email = ?");
$sqlLogin->bind_param("s",$loginEmail);
$sqlLogin->execute();
$sqlLogin = $sqlLogin->get_result();
$numrowsLogin = $sqlLogin->num_rows;
if($numrowsLogin == 1) {
$rowLogin = $sqlLogin->fetch_assoc();
$stored_password = $rowLogin['password'];
}
if(password_verify($loginPassword, $stored_password)){
header('Location: homepage.php');
}else{
echo 'invalid login';
}
}
?>
<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<table style="width:500px">
<tr>
<td width="30%"><input style="width: 200px; height: 25px; border-radius: 5px;" type="text" name="loginEmail" placeholder = "Email" required/><br/></td>
</tr>
<tr>
<td width="30%"><input style="width: 200px; height: 25px; border-radius: 5px;" type="password" name="loginPassword" placeholder = "Password" required/><br/></td>
</tr>
</table>
<input style="font-weight: bold; width: 70px; height: 25px; border-radius: 5px;" type="submit" name="login" value="Login"/>
</form>
</body>
</html>
完成任何基本的調試,如檢查您的查詢是否成功並返回存儲的散列?並且請注意,在表單的'action'中使用'$ _SERVER ['PHP_SELF']'就容易受到XSS攻擊。 –
您需要在任何'header('location:...');'-call'之後添加'exit;',因爲我們此時想停止向瀏覽器輸出內容。 –
是的,對不起,已更新 –