74 lines
2.3 KiB
PHP
74 lines
2.3 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 {
|
|
$stmt = db()->prepare("SELECT * FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user) {
|
|
$error = 'Username already exists.';
|
|
} else {
|
|
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = db()->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
if ($stmt->execute([$username, $password_hash])) {
|
|
$success = 'Registration successful. You can now <a href="login.php">login</a>.';
|
|
} else {
|
|
$error = 'An error occurred. Please try again.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<div class="container">
|
|
<h1 class="mt-5">Register</h1>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success" role="alert">
|
|
<?php echo $success; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST">
|
|
<div class="form-group">
|
|
<label for="username">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password_confirm">Confirm Password</label>
|
|
<input type="password" class="form-control" id="password_confirm" name="password_confirm" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Register</button>
|
|
</form>
|
|
<p class="mt-3">Already have an account? <a href="login.php">Login here</a>.</p>
|
|
</div>
|
|
|
|
<?php include 'footer.php'; ?>
|