35106-vm/auth/signup.php
2025-10-22 11:48:26 +00:00

154 lines
6.6 KiB
PHP

<?php
require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../db/config.php';
$errors = [];
$success = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Sanitize and retrieve form data
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$gender = $_POST['gender'] ?? '';
$year = $_POST['year'] ?? '';
$department = trim($_POST['department']);
// Validation
if (empty($name)) {
$errors[] = 'Name is required.';
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'A valid email is required.';
}
if (empty($password)) {
$errors[] = 'Password is required.';
}
if ($password !== $confirm_password) {
$errors[] = 'Passwords do not match.';
}
if (empty($gender)) {
$errors[] = 'Gender is required.';
}
if (empty($year)) {
$errors[] = 'Year is required.';
}
if (empty($department)) {
$errors[] = 'Department is required.';
}
if (empty($errors)) {
try {
$pdo = db();
// Check if email already exists
$stmt = $pdo->prepare("SELECT COUNT(*) FROM Users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetchColumn() > 0) {
$errors[] = 'Email address is already registered.';
} else {
// Hash password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Set default role
$role = 'Student';
// Insert user into database
$sql = "INSERT INTO Users (name, email, password, role, gender, year, department) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
if ($stmt->execute([$name, $email, $hashed_password, $role, $gender, $year, $department])) {
$success = 'Registration successful! You can now <a href="login.php">log in</a>.';
// Send welcome email
require_once __DIR__ . '/../mail/MailService.php';
$subject = 'Welcome to Student Hostel';
$body = "<h1>Welcome, {$name}!</h1><p>Your account has been successfully created. You can now log in and request a room.</p>";
MailService::sendMail($email, $subject, $body);
} else {
$errors[] = 'Something went wrong. Please try again later.';
}
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h2>Sign Up</h2>
</div>
<div class="card-body">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success">
<p><?php echo $success; ?></p>
</div>
<?php else: ?>
<form action="signup.php" method="post">
<div class="form-group mb-3">
<label for="name">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group mb-3">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" 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>
<div class="form-group mb-3">
<label for="confirm_password">Confirm Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
</div>
<div class="form-group mb-3">
<label for="gender">Gender</label>
<select class="form-control" id="gender" name="gender" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group mb-3">
<label for="year">Year</label>
<select class="form-control" id="year" name="year" required>
<option value="">Select Year</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
<option value="5">Fifth</option>
</select>
</div>
<div class="form-group mb-3">
<label for="department">Department</label>
<input type="text" class="form-control" id="department" name="department" required>
</div>
<button type="submit" class="btn btn-primary w-100">Sign Up</button>
</form>
<?php endif; ?>
</div>
<div class="card-footer text-center">
<p>Already have an account? <a href="login.php">Login here</a>.</p>
</div>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>