77 lines
2.9 KiB
PHP
77 lines
2.9 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 {
|
|
$pdo = db();
|
|
$stmt = $pdo->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 = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
if ($stmt->execute([$username, $hashed_password])) {
|
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
|
$_SESSION['username'] = $username;
|
|
header("Location: index.php");
|
|
exit;
|
|
} else {
|
|
$error = 'Something went wrong. Please try again.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Register - Avatar Generator</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/feather-icons/4.29.0/feather.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<div class="auth-container">
|
|
<div class="auth-card">
|
|
<h1 class="auth-title">Create Account</h1>
|
|
<p class="auth-subtitle">Get started with your own custom avatars.</p>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="register.php">
|
|
<div class="form-group">
|
|
<label for="username">Username</label>
|
|
<input type="text" id="username" name="username" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary btn-gradient">Register</button>
|
|
</div>
|
|
</form>
|
|
<div class="auth-switch">
|
|
<p>Already have an account? <a href="login.php">Log In</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/feather-icons/4.29.0/feather.min.js"></script>
|
|
<script>
|
|
feather.replace();
|
|
</script>
|
|
</body>
|
|
</html>
|