71 lines
2.5 KiB
PHP
71 lines
2.5 KiB
PHP
<?php
|
|
session_start();
|
|
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 = "INSERT INTO users (username, password) VALUES (:username, :password)";
|
|
$stmt = $conn->prepare($sql);
|
|
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
$stmt->bindParam(':username', $username);
|
|
$stmt->bindParam(':password', $hashed_password);
|
|
|
|
if ($stmt->execute()) {
|
|
header("location: login.php");
|
|
} else {
|
|
$error = "Something went wrong. Please try again later.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
if ($e->errorInfo[1] == 1062) { // Duplicate entry
|
|
$error = "This username is already taken.";
|
|
} else {
|
|
$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>Register - 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">Register</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">Register</button>
|
|
</div>
|
|
<p class="mt-3 text-center">Already have an account? <a href="login.php">Login here</a>.</p>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|