82 lines
3.4 KiB
PHP
82 lines
3.4 KiB
PHP
|
|
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = 'Please fill in all fields.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Username already taken.';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
$stmt->execute([$username, $hashed_password]);
|
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
|
$_SESSION['username'] = $username;
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html class="dark" lang="en">
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
|
|
<title>Register - AD Messaging App</title>
|
|
<script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet"/>
|
|
<style>
|
|
body {
|
|
font-family: 'Inter', sans-serif;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body class="bg-background-dark text-text-dark flex items-center justify-center h-screen">
|
|
<div class="w-full max-w-md p-8 space-y-8 bg-input-bg-dark rounded-xl shadow-lg">
|
|
<h2 class="text-2xl font-bold text-center">Create your account</h2>
|
|
<?php if ($error): ?>
|
|
<div class="p-4 text-sm text-red-700 bg-red-100 rounded-lg" role="alert">
|
|
<?= htmlspecialchars($error) ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form method="POST" action="register.php" class="space-y-6">
|
|
<div>
|
|
<label for="username" class="text-sm font-medium text-gray-300">Username</label>
|
|
<input id="username" name="username" type="text" required
|
|
class="w-full px-4 py-2 mt-2 text-white bg-gray-700 border border-gray-600 rounded-lg focus:ring-primary focus:border-primary">
|
|
</div>
|
|
<div>
|
|
<label for="password" class="text-sm font-medium text-gray-300">Password</label>
|
|
<input id="password" name="password" type="password" required
|
|
class="w-full px-4 py-2 mt-2 text-white bg-gray-700 border border-gray-600 rounded-lg focus:ring-primary focus:border-primary">
|
|
</div>
|
|
<div>
|
|
<button type="submit"
|
|
class="w-full px-4 py-2 font-semibold text-white bg-primary rounded-lg hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary dark:focus:ring-offset-background-dark">
|
|
Register
|
|
</button>
|
|
</div>
|
|
</form>
|
|
<p class="text-sm text-center text-gray-400">
|
|
Already have an account? <a href="login.php" class="font-medium text-primary hover:underline">Log in</a>
|
|
</p>
|
|
</div>
|
|
</body>
|
|
</html>
|