68 lines
2.6 KiB
PHP
68 lines
2.6 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'includes/header.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$username = trim($_POST['username']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = 'Please fill in all fields.';
|
|
} else {
|
|
// Check if username already exists
|
|
$stmt = db()->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Username already taken.';
|
|
} else {
|
|
// Insert new user
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = db()->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
if ($stmt->execute([$username, $hashed_password])) {
|
|
$success = 'Registration successful! You can now <a href="login.php">login</a>.';
|
|
} else {
|
|
$error = 'Something went wrong. Please try again.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card mt-5">
|
|
<div class="card-body">
|
|
<h3 class="card-title text-center">Register</h3>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><?php echo $success; ?></div>
|
|
<?php else: ?>
|
|
<form action="register.php" method="post">
|
|
<div class="form-group mb-3">
|
|
<label for="username">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" required>
|
|
</div>
|
|
<div class="form-group mb-3">
|
|
<label for="password">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Register</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<div class="text-center mt-3">
|
|
<p>Already have an account? <a href="login.php">Login here</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|