78 lines
3.1 KiB
PHP
78 lines
3.1 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$error_message = '';
|
|
$success_message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($username) || empty($email) || empty($password)) {
|
|
$error_message = 'Please fill in all fields.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error_message = 'Invalid email format.';
|
|
} else {
|
|
try {
|
|
$db = db();
|
|
|
|
// Check if email already exists
|
|
$stmt = $db->prepare("SELECT id FROM users WHERE email = :email");
|
|
$stmt->bindParam(':email', $email);
|
|
$stmt->execute();
|
|
|
|
if ($stmt->fetch()) {
|
|
$error_message = 'An account with this email already exists.';
|
|
} else {
|
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
$stmt = $db->prepare("INSERT INTO users (username, email, password_hash) VALUES (:username, :email, :password)");
|
|
$stmt->bindParam(':username', $username);
|
|
$stmt->bindParam(':email', $email);
|
|
$stmt->bindParam(':password', $password_hash);
|
|
|
|
if ($stmt->execute()) {
|
|
$success_message = 'Registration successful! You can now <a href="login.php">login</a>.';
|
|
} else {
|
|
$error_message = 'Registration failed. Please try again.';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
$pageTitle = "Register - Flatlogic";
|
|
include 'includes/header.php';
|
|
?>
|
|
<div class="container">
|
|
<header>
|
|
<h1>Register</h1>
|
|
</header>
|
|
<main>
|
|
<?php if ($error_message): ?>
|
|
<div class="message error"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($success_message): ?>
|
|
<div class="message success"><?php echo $success_message; ?></div>
|
|
<?php else: ?>
|
|
<form action="register.php" method="post" class="form-card">
|
|
<div class="form-group">
|
|
<label for="username">Username</label>
|
|
<input type="text" id="username" name="username" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="button">Register</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<p class="text-center">Already have an account? <a href="login.php">Login here</a>.</p>
|
|
</main>
|
|
<?php include 'includes/footer.php'; ?>
|