96 lines
3.8 KiB
PHP
96 lines
3.8 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$errors = [];
|
|
$success_message = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$display_name = trim($_POST['display_name']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($display_name)) {
|
|
$errors[] = 'Display name is required.';
|
|
}
|
|
|
|
if (empty($email)) {
|
|
$errors[] = 'Email is required.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'Invalid email format.';
|
|
}
|
|
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
} elseif (strlen($password) < 8) {
|
|
$errors[] = 'Password must be at least 8 characters long.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = 'Email address is already registered.';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (display_name, email, password_hash) VALUES (?, ?, ?)");
|
|
if ($stmt->execute([$display_name, $email, $hashed_password])) {
|
|
// Redirect to login page after successful registration
|
|
header("Location: login.php?registered=true");
|
|
exit;
|
|
} else {
|
|
$errors[] = 'Failed to create account. Please try again later.';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log($e->getMessage());
|
|
$errors[] = 'Database error. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<main class="container flex-grow-1 d-flex align-items-center justify-content-center">
|
|
<div class="col-md-6 col-lg-4">
|
|
<div class="card my-5">
|
|
<div class="card-body p-4">
|
|
<h1 class="card-title text-center mb-4">Create Account</h1>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="signup.php" method="post" novalidate>
|
|
<div class="mb-3">
|
|
<label for="displayName" class="form-label">Display Name</label>
|
|
<input type="text" class="form-control" id="displayName" name="display_name" required value="<?php echo isset($_POST['display_name']) ? htmlspecialchars($_POST['display_name']) : ''; ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email address</label>
|
|
<input type="email" class="form-control" id="email" name="email" placeholder="name@example.com" required value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" required>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">Sign Up</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<div class="card-footer text-center py-3">
|
|
<small class="text-muted">Already have an account? <a href="login.php">Sign In</a></small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include 'footer.php'; ?>
|