我有一个登录表单,当用户输入正确的用户名和密码时它可以正常工作但输入错误信息时没有任何反应。如何显示错误消息?我有“登录失败”,如下所示,但由于某种原因这不起作用。任何帮助非常感谢。
<?php
require("config.php");
$submitted_username = '';
if(!empty($_POST)){
$query = "SELECT
id,
username,
password,
salt,
email
FROM users
WHERE
username = :username";
$query_params = array(
':username' => $_POST['username']
);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
$login_ok = false;
$row = $stmt->fetch();
if($row){
$check_password = hash('sha256', $_POST['password'] . $row['salt']);
for($round = 0; $round < 65536; $round++){
$check_password = hash('sha256', $check_password . $row['salt']);
}
if($check_password === $row['password']){
$login_ok = true;
}
}
if($login_ok){
unset($row['salt']);
unset($row['password']);
$_SESSION['user'] = $row;
header("Location: secret.php");
die("Redirecting to: secret.php");
}
else{
print("Login Failed.");
$submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');
}
}
?>