30855-vm/register.php
Flatlogic Bot e62cdf478f v1
2025-09-10 17:18:29 +00:00

102 lines
3.9 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'templates/header.php';
$errors = [];
$username = '';
$email = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$password_confirm = $_POST['password_confirm'];
// Validation
if (empty($username)) {
$errors[] = 'Username 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';
}
if ($password !== $password_confirm) {
$errors[] = 'Passwords do not match';
}
// Check if user already exists
if (empty($errors)) {
$stmt = db()->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
$stmt->execute([$username, $email]);
if ($stmt->fetch()) {
$errors[] = 'Username or email already taken';
}
}
// Insert user if no errors
if (empty($errors)) {
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = db()->prepare("INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)");
if ($stmt->execute([$username, $email, $password_hash])) {
// Redirect to login page
header("Location: login.php?registration=success");
exit();
} else {
$errors[] = 'Something went wrong. Please try again.';
}
}
}
?>
<main class="container my-5">
<section class="py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="feature-card p-4">
<h2 class="text-center mb-4">Create an Account</h2>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p class="m-0"><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST" action="register.php">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($username); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</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="password_confirm" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="password_confirm" name="password_confirm" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Register</button>
</div>
</form>
<p class="text-center mt-3">
Already have an account? <a href="login.php">Login here</a>.
</p>
</div>
</div>
</div>
</div>
</section>
</main>
<?php require_once 'templates/footer.php'; ?>