107 lines
4.9 KiB
PHP
107 lines
4.9 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
$password_confirm = $_POST['password_confirm'] ?? '';
|
|
|
|
if (empty($username) || empty($password) || empty($password_confirm)) {
|
|
$error = 'Please fill in all fields.';
|
|
} elseif ($password !== $password_confirm) {
|
|
$error = 'Passwords do not match.';
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Username already exists.';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = db()->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
$stmt->execute([$username, $hashed_password]);
|
|
$success = 'Registration successful! You can now <a href="login.php" class="font-bold text-blue-700">log in</a>.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
if ($e->getCode() === '42S02') { // Base table not found
|
|
$error = 'The application has not been fully set up. Please run the database migrations.';
|
|
} else {
|
|
$error = "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 - My Blog</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Georgia&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body class="bg-light-gray">
|
|
|
|
<header class="bg-white shadow-md">
|
|
<div class="container mx-auto px-4 py-6">
|
|
<h1 class="text-3xl font-serif text-dark-gray"><a href="index.php">My Blog</a></h1>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container mx-auto px-4 py-12">
|
|
<div class="max-w-md mx-auto bg-white p-8 rounded-lg shadow-md">
|
|
<h2 class="text-2xl font-bold mb-6 text-center">Create an Account</h2>
|
|
<?php if ($error): ?>
|
|
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
|
|
<span class="block sm:inline"><?php echo htmlspecialchars($error); ?></span>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($success): ?>
|
|
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
|
|
<span class="block sm:inline"><?php echo $success; ?></span>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="register.php" method="POST">
|
|
<div class="mb-4">
|
|
<label for="username" class="block text-gray-700 text-sm font-bold mb-2">Username</label>
|
|
<input type="text" id="username" name="username" required
|
|
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="password" class="block text-gray-700 text-sm font-bold mb-2">Password</label>
|
|
<input type="password" id="password" name="password" required
|
|
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
|
</div>
|
|
<div class="mb-6">
|
|
<label for="password_confirm" class="block text-gray-700 text-sm font-bold mb-2">Confirm Password</label>
|
|
<input type="password" id="password_confirm" name="password_confirm" required
|
|
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline">
|
|
</div>
|
|
<div class="flex items-center justify-between">
|
|
<button type="submit"
|
|
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
|
|
Register
|
|
</button>
|
|
<a href="login.php" class="inline-block align-baseline font-bold text-sm text-blue-500 hover:text-blue-800">
|
|
Login
|
|
</a>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="bg-white mt-12">
|
|
<div class="container mx-auto px-4 py-6 text-center text-gray-600">
|
|
<p>© <?php echo date('Y'); ?> My Blog. All rights reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
</body>
|
|
</html>
|