我是 PHP 新手,我一直在学习有关如何制作注册/登录页面的课程。我已成功完成注册页面,但登录页面出现问题。这是我的 login.php 代码:
<?php
include_once '../resources/session.php';
include_once '../resources/database.php';
include_once '../resources/utilities.php';
if(isset($_POST['loginBtn'])){
// array to hold errors
$form_errors = array();
// validate
$required_fields = array('username ', 'password ');
$form_errors = array_merge($form_errors, check_empty_fields($required_fields));
if(empty($form_errors)){
// collect form data
$user = $_POST['username'];
$password = $_POST['password'];
// check if user exists in the database
$sqlQuery = "SELECT * FROM users WHERE username = :username";
$statement = $db->prepare($sqlQuery);
$statement->execute(array(':username' => $user));
while($row = $statement->fetch()){
$id = $row['id'];
$hashedpassword = $row['password'];
$username = $row['username'];
if(password_verify($password, $hashed_password)){
$_SESSION['id'] = $id;
$_SESSION['username'] = $username;
header("location: dashboard.php");
}
else{
$result ="<p style='padding: 20px; color: red; border: 1px solid gray;'> Invalid username or password</p>";
}
}
}
else{
if(count($form_errors) == 1){
$result = "<p style='color:red;'>There was 1 error in the form</p>";
}
else{
$result = "<p style='color:red;'> There were " .count($form_errors). " errors in the form </p>";
}
}
}
?>
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="../css/indexstyles.css">
</head>
<body>
<h2>Login Form</h2>
<?php if(isset($result)) echo $result; ?>
<?php if(!empty($form_errors)) echo show_errors($form_errors); ?>
当我按下登录按钮而不输入任何内容时,我收到预期的错误消息。但是,如果我输入我创建的模拟帐户,我仍然收到相同的错误消息。
杨魅力