102 lines
4.2 KiB
PHP
102 lines
4.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once 'includes/header.php';
|
|
|
|
$errors = [];
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$name = trim($_POST['name']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
$confirm_password = $_POST['confirm_password'];
|
|
|
|
if (empty($name)) {
|
|
$errors[] = "Name is required.";
|
|
}
|
|
|
|
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = "A valid email is required.";
|
|
}
|
|
|
|
if (empty($password) || strlen($password) < 8) {
|
|
$errors[] = "Password must be at least 8 characters long.";
|
|
}
|
|
|
|
if ($password !== $confirm_password) {
|
|
$errors[] = "Passwords do not match.";
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = "An account with this email already exists.";
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
|
|
if ($stmt->execute([$name, $email, $hashed_password])) {
|
|
header("Location: login.php");
|
|
exit;
|
|
} else {
|
|
$errors[] = "Something went wrong. Please try again later.";
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<main class="py-5">
|
|
<section class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-6">
|
|
<div class="card">
|
|
<div class="card-body p-5">
|
|
<h2 class="card-title text-center mb-4">Create Your Account</h2>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="register.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Full Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['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" 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="mb-3">
|
|
<label for="confirm_password" class="form-label">Confirm Password</label>
|
|
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary btn-lg">Register</button>
|
|
</div>
|
|
</form>
|
|
<p class="text-center mt-4">
|
|
Already have an account? <a href="login.php">Log In</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|