3 Commits
4 changed files with 51 additions and 30 deletions
@@ -31,7 +31,7 @@ CREATE TABLE IF NOT EXISTS `login_users` (
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `login_users` (`id`, `username`, `password`) VALUES
(1, 'u1', 'p1');
(1, 'u1', '$2y$10$L18u5/PyVkDgsce/DsUOQu0sKhTzh854Euhog3cVb1W4YAfgRzY8W'); /* php -r 'echo password_hash("p1", PASSWORD_DEFAULT), PHP_EOL;' */
CREATE TABLE IF NOT EXISTS `notes` (
`notesid` int(11) NOT NULL AUTO_INCREMENT,
+11 -4
View File
@@ -99,16 +99,23 @@ $stmt->close();
//echo htmlspecialchars($username);
echo "<h3>Entries of " . $username . "</h3>";
$safe_username = htmlspecialchars($username, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
echo "<h3>Entries of " . $safe_username . "</h3>";
if (!empty($result) && $result->num_rows >= 1) {
while ($row = $result -> fetch_assoc()) {
// Escape output to prevent stored XSS (DB content must be treated as untrusted).
$safe_url = htmlspecialchars($row["web_url"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
$safe_user = htmlspecialchars($row["web_username"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
$safe_pass = htmlspecialchars($row["web_password"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
$webid_safe = (int)$row["webid"];
echo "<table border=0>";
echo "<tr style='background-color: #f4f4f4;'><td colspan=2>" . $row["web_url"] . "</td></tr>" .
"<tr><td>Username: " . $row["web_username"] . "</td><td>Password: " . $row["web_password"] . "</td></tr>";
echo "<tr style='background-color: #f4f4f4;'><td colspan=2>" . $safe_url . "</td></tr>" .
"<tr><td>Username: " . $safe_user . "</td><td>Password: " . $safe_pass . "</td></tr>";
echo "<tr><td><form method='POST' style='height: 3px'>" .
"<input type='hidden' name='websiteid' value='" . $row["webid"] . "'>" .
"<input type='hidden' name='websiteid' value='" . $webid_safe . "'>" .
"<button type='submit' name='delete_website'>Delete</button></form></td></tr>";
echo "<tr><td colspan=2 style=height: 20px;></td></tr>";
+28 -23
View File
@@ -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();
}
}
?>
+11 -2
View File
@@ -37,8 +37,17 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
$login_message = "Database error (prepare failed).";
$result = false;
} else {
$stmt->bind_param("ss", $new_username, $new_password);
$result = $stmt->execute();
// Hash the password before storing it.
// Never store login passwords in plaintext.
$password_hash = password_hash($new_password, PASSWORD_DEFAULT);
if ($password_hash === false) {
$login_message = "Password hashing failed.";
$result = false;
} else {
// Store the hash (not the plaintext password).
$stmt->bind_param("ss", $new_username, $password_hash);
$result = $stmt->execute();
}
$stmt->close();
}