36629-vm/register.php
Flatlogic Bot ea73dc7483 v1
2025-12-04 08:49:26 +00:00

110 lines
4.7 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$error = '';
$success = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username']);
$password = $_POST['password'];
$role = $_POST['role'];
if (empty($username) || empty($password) || empty($role)) {
$error = 'Please fill in all fields.';
} elseif (strlen($password) < 6) {
$error = 'Password must be at least 6 characters long.';
} elseif ($role !== 'user' && $role !== 'psychologist') {
$error = 'Invalid role selected.';
} else {
try {
$pdo = db();
// Check if username already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$stmt->execute([$username]);
if ($stmt->fetch()) {
$error = 'Username already taken.';
} else {
// Hash the password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Insert new user
$insertStmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
if ($insertStmt->execute([$username, $hashed_password, $role])) {
$success = 'Registration successful! You can now <a href="login.php">login</a>.';
} else {
$error = 'Something went wrong. Please try again later.';
}
}
} catch (PDOException $e) {
$error = 'Database error. Please try again later.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Psychological Testing System</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light mb-5">
<div class="container">
<a class="navbar-brand fw-bold" href="index.php">Psychological Testing System</a>
</div>
</nav>
<main class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body p-5">
<h1 class="text-center fw-bold mb-4">Create an Account</h1>
<?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="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="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-4">
<label for="role" class="form-label">I am a...</label>
<select class="form-select" id="role" name="role">
<option value="user" selected>User (taking tests)</option>
<option value="psychologist">Psychologist (creating tests)</option>
</select>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Register</button>
</div>
</form>
<?php endif; ?>
<div class="text-center mt-4">
<p>Already have an account? <a href="login.php">Login here</a></p>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="py-5 mt-5 text-center text-secondary">
<p>&copy; <?php echo date('Y'); ?> Psychological Testing System. All rights reserved.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>