35595-vm/register.php
Flatlogic Bot 89d2171b50 1.0.1
2025-11-09 14:00:35 +00:00

114 lines
4.2 KiB
PHP

<?php
require_once 'header.php';
require_once 'db/config.php';
$errors = [];
$username = '';
$email = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password_confirm = $_POST['password_confirm'];
if (empty($username)) {
$errors[] = 'Username is required';
}
if (empty($email)) {
$errors[] = 'Email is required';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email format';
}
if (empty($password)) {
$errors[] = 'Password is required';
}
if ($password !== $password_confirm) {
$errors[] = 'Passwords do not match';
}
if (empty($errors)) {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
$stmt->execute([$username, $email]);
$existing_user = $stmt->fetch();
if ($existing_user) {
if ($existing_user['username'] === $username) {
$errors[] = 'Username already exists';
}
if ($existing_user['email'] === $email) {
$errors[] = 'Email already exists';
}
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
if ($stmt->execute([$username, $email, $hashed_password])) {
// Get the new user's ID
$user_id = $pdo->lastInsertId();
// Start session and store user info
$_SESSION['user_id'] = $user_id;
$_SESSION['username'] = $username;
$_SESSION['role'] = 'user'; // Default role
header("Location: profile.php");
exit;
} else {
$errors[] = 'Failed to register user. Please try again.';
}
}
}
}
?>
<header class="hero text-center">
<div class="container">
<h1 class="display-4">Register</h1>
<p class="lead">Create an account to get started.</p>
</div>
</header>
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="card">
<div class="card-body p-5">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="register.php" method="POST">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($username); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3">
<label for="password_confirm" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="password_confirm" name="password_confirm" required>
</div>
<button type="submit" class="btn btn-primary w-100">Register</button>
</form>
</div>
</div>
</div>
</div>
</main>
<?php require_once 'footer.php'; ?>