103 lines
3.9 KiB
PHP
103 lines
3.9 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/common.php';
|
|
require_guest();
|
|
|
|
$title = 'Register';
|
|
$errors = [];
|
|
$username = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
$password_confirm = $_POST['password_confirm'] ?? '';
|
|
|
|
if (empty($username)) {
|
|
$errors[] = 'Username is required.';
|
|
} elseif (strlen($username) < 3) {
|
|
$errors[] = 'Username must be at least 3 characters long.';
|
|
}
|
|
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
} elseif (strlen($password) < 6) {
|
|
$errors[] = 'Password must be at least 6 characters long.';
|
|
}
|
|
|
|
if ($password !== $password_confirm) {
|
|
$errors[] = 'Passwords do not match.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = 'Username is already taken.';
|
|
} else {
|
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, password_hash) VALUES (?, ?)");
|
|
$stmt->execute([$username, $password_hash]);
|
|
|
|
// Log the user in
|
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
|
$_SESSION['username'] = $username;
|
|
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors[] = 'Database error. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
|
|
require_once __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-6 col-md-8">
|
|
<div class="card">
|
|
<div class="card-body p-5">
|
|
<h1 class="card-title text-center mb-4">Create an Account</h1>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?= htmlspecialchars($error) ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="register.php" method="POST" class="needs-validation" novalidate>
|
|
<div class="form-floating mb-3">
|
|
<input type="text" class="form-control" id="username" name="username" placeholder="Username" value="<?= htmlspecialchars($username) ?>" required>
|
|
<label for="username">Username</label>
|
|
<div class="invalid-feedback">Please enter a username.</div>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
|
|
<label for="password">Password</label>
|
|
<div class="invalid-feedback">Please enter a password.</div>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<input type="password" class="form-control" id="password_confirm" name="password_confirm" placeholder="Confirm Password" required>
|
|
<label for="password_confirm">Confirm Password</label>
|
|
<div class="invalid-feedback">Please confirm your password.</div>
|
|
</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">Login here</a>.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
require_once __DIR__ . '/includes/footer.php';
|
|
?>
|