108 lines
4.2 KiB
PHP
108 lines
4.2 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$errors = [];
|
|
$success = '';
|
|
|
|
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)) {
|
|
$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 ($password !== $confirm_password) {
|
|
$errors[] = 'Passwords do not match';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$existing_user = $stmt->fetch();
|
|
|
|
if ($existing_user) {
|
|
$errors[] = 'Email already exists';
|
|
} else {
|
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (name, email, password_hash) VALUES (?, ?, ?)");
|
|
if ($stmt->execute([$name, $email, $password_hash])) {
|
|
$success = "Registration successful! You can now <a href='login.php'>login</a>.";
|
|
} else {
|
|
$errors[] = 'Failed to register. Please try again.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
require_once 'includes/header.php';
|
|
?>
|
|
|
|
<div class="container mt-5 pt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h3 class="text-center">Register</h3>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p><?php echo $error; ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($success)): ?>
|
|
<div class="alert alert-success">
|
|
<p><?php echo $success; ?></p>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="register.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">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">Register</button>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="card-footer text-center">
|
|
<p>Already have an account? <a href="login.php">Login here</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|