churchtube/register.php

77 lines
2.8 KiB
PHP

<?php
require_once 'includes/db.php';
require_once 'includes/auth.php';
if (isLoggedIn()) {
header('Location: index.php');
exit;
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if ($password !== $confirm_password) {
$error = "Passwords do not match.";
} else {
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
$stmt->execute([$username, $email]);
if ($stmt->fetch()) {
$error = "Username or Email already exists.";
} else {
$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'user')");
if ($stmt->execute([$username, $email, $hash])) {
header('Location: login.php?registered=1');
exit;
} else {
$error = "Registration failed. Please try again.";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register | ChurchTube</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="centered-container">
<a href="index.php" class="logo" style="display: block; text-align: center; margin-bottom: 24px;">ChurchTube</a>
<h2>Create Account</h2>
<?php if ($error): ?>
<div style="color: #ff4081; margin-bottom: 16px;"><?= $error ?></div>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label class="form-label">Username</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label class="form-label">Email</label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label class="form-label">Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="form-group">
<label class="form-label">Confirm Password</label>
<input type="password" name="confirm_password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">Register</button>
</form>
<p style="margin-top: 20px; text-align: center; color: var(--text-muted);">
Already have an account? <a href="login.php" style="color: var(--primary-color);">Login</a>
</p>
</div>
</body>
</html>