Итак, у меня есть страница входа с использованием приведенных ниже кодов, однако код проверки не работает должным образом, он либо позволяет мне войти с любым паролем, либо вообще не позволяет.
инициал.php:
<?php
date_default_timezone_set('Asia/Riyadh');
$error = ['Username' => '', 'Email' => '', 'Password' => ''];
$input = ['Username' => '', 'Email' => '', 'Password' => ''];
session_start();
$config = require 'config.php';
$db = new mysqli(...$config['db']);
$db->set_charset($config['db_charset']);
?>
Регистрация.php:
<?php
require 'init.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// input validation
$Username = $input['Username'] = trim(filter_input(INPUT_POST, 'Username'));
if (mb_strlen($Username) < 3 || mb_strlen($Username) > 30) {
$error['Username'] = 'Please enter your name, it must be from 3 to 30 charaters long.';
echo "<p class='Center'> <font color=White size='50pt'>Username should be at least 3 characters long!</font> </p>";
}
$Email = $input['Email'] = trim(filter_input(INPUT_POST, 'Email'));
if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
$error['Email'] = 'Please enter a valid email address.';
echo "<p class='Center'> <font color=White size='50pt'>Please enter a valid email address!</font> </p>";
} else {
$result = $db->execute_query("SELECT 1 FROM users WHERE email = ?", [$Email]);
if ($result->fetch_row()) {
$error['Email'] = 'Email address already taken.';
echo "<p class='Center'> <font color=White size='50pt'>Email address already taken.Please Login!</font> </p>";
}
}
$Password = filter_input(INPUT_POST, 'Password');
if (strlen($Password) < 3 || strlen($Password) > 72) {
$error['Password'] = 'Please enter password, it must be from 3 to 72 characters long.';
echo "<p class='Center'> <font color=White size='50pt'>Password should be at least 3 characters long!</font> </p>";
}
// if no errors
if (implode("", $error) === '')
{
// Password MUST be hashed using the dedicated function
$Password = password_hash($input['Password'], PASSWORD_DEFAULT);
$VIP= "NO";
$Admin = "NO";
$Creation_date = date('d-M-Y h:i:s A');
$Last_Login = date('d-M-Y h:i:s A');
$Login_Times=1;
// a parameterized query MUST be used to avoid errors and injections
$stmt = $db->prepare("INSERT INTO Users (Username, Email, Password, VIP, Admin, Creation_Date, Last_login, Login_Times) VALUES (?,?,?,?,?,?,?,?)");
$stmt->execute([
$Username,
$Email,
$Password,
$VIP,
$Admin,
$Creation_date,
$Last_Login,
$Login_Times,
]);
echo "<p class='Center'> <font color=White size='50pt'>Registeration successful</font> </p>";
$_SESSION['Email'] = $Email;
header("Location: home.php");
die;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv = "refresh" content = "4;URL=login.php">
<link rel = "stylesheet" href = "Styles/General.css">
<link rel = "stylesheet" href = "Styles/Background.css">
<link rel = "icon" href = "favicon.ico" type = "image/x-icon" /> <link rel = "shortcut icon" href = "favicon.ico" type = "image/x-icon" />
<title>Register & Login</title>
</head>
<body class = "Blue-Black">
<h1 class = "Center">Please wait, you will be automatically redirected to the login & registeration page.</h1>
</body>
</html>
Validation.php: (вот в чем проблема)
<?php
require 'init.php';
$Email = $input['Email'] = trim(filter_input(INPUT_POST, 'Email'));
// i use one of the 3 below
$Password = $input['Password']; //if i use this it lets me in with any password
$Password = $_POST['Password']; // if i use this it doesn't let me in at all
$Password = filter_input(INPUT_POST, 'Password'); // if i add this to 1/ if i don't it still doesn't let me in
$result = $db->execute_query("SELECT Email FROM Users WHERE Email = ?", [$Email]);
if ($result->fetch_row()) {
$select = "SELECT Password FROM Users WHERE Email = ?;";
$result2 = $db ->execute_query($select, [$Email]) ;
$Get_hash = $result2 ->fetch_assoc();
$hash = $Get_hash['Password'];
if (password_verify($Password, $hash)) {
$_SESSION['Email'] = $Email;
$Date = date('d-M-Y h:i A');
$Update = "UPDATE Users SET Last_Login = ?, Login_Times = Login_Times + 1 WHERE Users.Email = ?";
$stmt = $db->execute_query($Update, [$Date, $Email]);
header('location:home.php');
}else{
echo "<p class='Center'> <font color=White size='50pt'>Invalid Password. Try again!</font> </p>";
}
}else{
echo "<p class='Center'> <font color=White size='50pt'>There is no account associated with this email address please sign up!</font> </p>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv = "refresh" content = "4;URL=login.php">
<link rel = "stylesheet" href = "Styles/General.css">
<link rel = "stylesheet" href = "Styles/Background.css">
<link rel = "icon" href = "favicon.ico" type = "image/x-icon" /> <link rel = "shortcut icon" href = "favicon.ico" type = "image/x-icon" />
<title>Register & Login</title>
</head>
<body class = "Blue-Black">
<h1 class = "Center">Please wait, you will be automatically redirected to the login & registeration page.</h1>
</body>
</html>
Логин.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1">
<link rel = "stylesheet" href = "Styles/Bootstrap.css">
<link rel = "stylesheet" href = "Styles/Font-Awesome.css">
<link rel = "stylesheet" href = "Styles/General.css">
<link rel = "stylesheet" href = "Styles/Login.css">
<link rel = "icon" href = "favicon.ico" type = "image/x-icon" /> <link rel = "shortcut icon" href = "favicon.ico" type = "image/x-icon" />
<title>Register & Login</title>
</head>
<body>
<a class = "btn btn-danger" href = "index.html">Cancel</a>
<div>
<h1 class = "px20">Please Login after you Register if you don't have an account.</h1>
<div>
<div class = "form-box">
<div class = "button-box">
<div id = "btn"></div>
<button type = "button" class = "toggle-btn" onclick = "Login()">
Login
</button>
<button type = "button" class = "toggle-btn" onclick = "Register()">
Register
</button>
</div>
<form action = "validation.php" method = "POST" id = "Login" class = "input-group">
<input type = "Email" name = "Email" class = "input-field" placeholder = "Email" required>
<input type = "Password" name = "Password" id = "Password" class = "input-field" placeholder = "Password" required>
<i class = "far fa-eye" id = "togglePassword"></i>
<button type = "submit" class = "submit-btn">Login</button>
</form>
<form action = "registeration.php" method = "POST" id = "Register" class = "input-group">
<input type = "Email" name = "Email" class = "input-field" placeholder = "Email" required>
<input type = "text" name = "Username" class = "input-field" placeholder = "Username" required>
<input type = "Password" name = "Password" class = "input-field" id = "Password2" placeholder = "Password" required>
<i class = "far fa-eye" id = "togglePassword2"></i>
<button type = "submit" class = "submit-btn">Register</button>
</form>
</div>
</div>
</div>
<script src = "Scripts/Login.js"></script>
</body>
</html>
что-то не так, но я не могу это понять (ПРИМЕЧАНИЕ: я новичок в php и mysql, поэтому буду признателен за подробное объяснение, и, пожалуйста, не судите, я все еще учусь, поэтому ошибка может быть несложной для профессионала






Вы не хэшируете пароль пользователя, вы хэшируете пустой пароль по умолчанию в $input (для чего вообще нужна эта переменная?). Изменять
$Password = password_hash($input['Password'], PASSWORD_DEFAULT);
к
$Password = password_hash($Password, PASSWORD_DEFAULT);
Это не «он» что-то хэширует. Он просто собирает код, который пишут для него разные люди.