30790-vm/login.php
Flatlogic Bot df71b56319 main
2025-09-04 13:24:51 +00:00

85 lines
3.0 KiB
PHP

<?php
session_start();
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
header("location: index.php");
exit;
}
require_once 'db/config.php';
$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (empty($username) || empty($password)) {
$error = 'Please enter username and password.';
} else {
try {
$conn = db();
$sql = "SELECT id, username, password FROM users WHERE username = :username";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
if ($stmt->execute()) {
if ($stmt->rowCount() == 1) {
$row = $stmt->fetch();
$id = $row['id'];
$hashed_password = $row['password'];
if (password_verify($password, $hashed_password)) {
session_start();
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
header("location: index.php");
} else {
$error = 'The password you entered was not valid.';
}
} else {
$error = 'No account found with that username.';
}
} else {
$error = "Oops! Something went wrong. Please try again later.";
}
} catch (PDOException $e) {
$error = "Error: " . $e->getMessage();
}
$conn = null;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Pet Tinder</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<h1 class="text-center mt-5">Login</h1>
<?php if(!empty($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="mt-4">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" name="username" id="username" class="form-control">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" name="password" id="password" class="form-control">
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Login</button>
</div>
<p class="mt-3 text-center">Don't have an account? <a href="register.php">Sign up now</a>.</p>
</form>
</div>
</body>
</html>