Fix plain text in user authendication.
This commit is contained in:
@@ -26,46 +26,51 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
// }
|
||||
require_once __DIR__ . "/config.php";
|
||||
|
||||
// SQL injection mitigation: use a prepared statement with bound parameters.
|
||||
// User input is treated strictly as data, not as part of the SQL syntax.
|
||||
$stmt = $conn->prepare("SELECT id FROM login_users WHERE username = ? AND password = ?");
|
||||
|
||||
// Authentication with hashed passwords:
|
||||
// 1) Fetch the stored hash by username
|
||||
// SQL injection mitigation: use a prepared statement with bound parameters.
|
||||
// User input is treated strictly as data, not as part of the SQL syntax.
|
||||
// 2) Verify the submitted password with password_verify()
|
||||
$stmt = $conn->prepare("SELECT id, password FROM login_users WHERE username = ?");
|
||||
if ($stmt === false) {
|
||||
// Fail closed (do not leak details in production).
|
||||
die("Prepare failed.");
|
||||
}
|
||||
|
||||
$stmt->bind_param("ss", $username, $password);
|
||||
$stmt->bind_param("s", $username);
|
||||
$stmt->execute();
|
||||
$stmt->store_result(); // Needed to use $stmt->num_rows
|
||||
|
||||
$result = $stmt->get_result(); // Requires mysqlnd (usually enabled)
|
||||
unset($_POST['username']);
|
||||
unset($_POST['password']);
|
||||
|
||||
if ($stmt->num_rows >= 1) {
|
||||
// Regenerate session ID to prevent session fixation!
|
||||
//session_regenerate_id(true);
|
||||
if ($result && $result->num_rows === 1) {
|
||||
$row = $result->fetch_assoc();
|
||||
$stored_hash = $row["password"];
|
||||
|
||||
// Successfully logged in
|
||||
$_SESSION['username'] = $username;
|
||||
$_SESSION['loggedin'] = true;
|
||||
// Verify password against the stored hash.
|
||||
if (password_verify($password, $stored_hash)) {
|
||||
// Regenerate session ID to prevent session fixation!
|
||||
//session_regenerate_id(true);
|
||||
|
||||
//while ($row = $result -> fetch_assoc()) {
|
||||
// print_r($row);
|
||||
// $_SESSION['user_id'] = $row['id'];
|
||||
//}
|
||||
// Successfully logged in
|
||||
$_SESSION['username'] = $username;
|
||||
$_SESSION['loggedin'] = true;
|
||||
|
||||
// Close
|
||||
$stmt->close();
|
||||
$conn -> close();
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
|
||||
// Redirect to a dashboard page
|
||||
header("Location: dashboard.php");
|
||||
exit;
|
||||
header("Location: dashboard.php");
|
||||
exit;
|
||||
} else {
|
||||
$login_message = "Invalid username or password";
|
||||
}
|
||||
} else {
|
||||
$login_message = "Invalid username or password";
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
$conn -> close();
|
||||
$conn->close();
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user