58 lines
2.2 KiB
PHP
58 lines
2.2 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$message = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$username = $_POST['username'];
|
|
$email = $_POST['email'];
|
|
$password = $_POST['password'];
|
|
$confirm_password = $_POST['confirm_password'];
|
|
|
|
if ($password !== $confirm_password) {
|
|
$message = '<div class="alert alert-danger">Passwords do not match.</div>';
|
|
} else {
|
|
$pdo = db();
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$message = '<div class="alert alert-danger">Email already registered.</div>';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, 'member')");
|
|
if ($stmt->execute([$username, $email, $hashed_password])) {
|
|
$message = '<div class="alert alert-success">Registration successful! You can now <a href="login.php">login</a>.</div>';
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Registration failed. Please try again.</div>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<?php require_once 'header.php'; ?>
|
|
|
|
<h2>Sign Up</h2>
|
|
<?php echo $message; ?>
|
|
<form action="signup.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" 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" 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="confirm_password" class="form-label">Confirm Password</label>
|
|
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Sign Up</button>
|
|
</form>
|
|
|
|
<?php require_once 'footer.php'; ?>
|