103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$errors = [];
|
|
$success = false;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($username)) {
|
|
$errors[] = 'Username 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 (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username OR email = :email");
|
|
$stmt->execute(['username' => $username, 'email' => $email]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = 'Username or email already exists.';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (:username, :email, :password)");
|
|
$stmt->execute(['username' => $username, 'email' => $email, 'password' => $hashed_password]);
|
|
$success = true;
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors[] = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Register - MusicBox</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<header class="app-header">
|
|
<div class="logo">
|
|
<h1><a href="index.php">MusicBox</a></h1>
|
|
</div>
|
|
<nav>
|
|
<a href="login.php" class="btn">Login</a>
|
|
</nav>
|
|
</header>
|
|
|
|
<main class="container">
|
|
<section class="auth-form">
|
|
<h2>Create an Account</h2>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success">
|
|
<p>Registration successful! You can now <a href="login.php">login</a>.</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="register.php" method="POST">
|
|
<div class="form-group">
|
|
<label for="username">Username</label>
|
|
<input type="text" id="username" name="username" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" minlength="8" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Register</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</section>
|
|
</main>
|
|
|
|
<footer class="app-footer">
|
|
<p>© <?php echo date('Y'); ?> MusicBox. All rights reserved.</p>
|
|
</footer>
|
|
</body>
|
|
</html>
|