Spaces:
Sleeping
Sleeping
| // Include database connection | |
| require_once 'includes/db_connect.php'; | |
| // Start session if not already started | |
| if (session_status() === PHP_SESSION_NONE) { | |
| session_start(); | |
| } | |
| // Check if user is already logged in | |
| if (isset($_SESSION['user_id'])) { | |
| header('Location: index.php'); | |
| exit; | |
| } | |
| $error = ''; | |
| $success = ''; | |
| // Process registration form | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
| // Validate inputs | |
| $username = $conn->real_escape_string(trim($_POST['username'])); | |
| $email = $conn->real_escape_string(trim($_POST['email'])); | |
| $password = trim($_POST['password']); | |
| $confirm_password = trim($_POST['confirm_password']); | |
| $user_type = $conn->real_escape_string(trim($_POST['user_type'])); | |
| // Validation | |
| if (empty($username) || empty($email) || empty($password) || empty($confirm_password) || empty($user_type)) { | |
| $error = "All fields are required."; | |
| } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
| $error = "Please enter a valid email address."; | |
| } elseif (strlen($password) < 6) { | |
| $error = "Password must be at least 6 characters long."; | |
| } elseif ($password !== $confirm_password) { | |
| $error = "Passwords do not match."; | |
| } elseif ($user_type !== 'student' && $user_type !== 'faculty') { | |
| $error = "Invalid user type."; | |
| } else { | |
| // Check if username already exists | |
| $check_query = "SELECT * FROM users WHERE username = ? OR email = ?"; | |
| $check_stmt = $conn->prepare($check_query); | |
| $check_stmt->bind_param("ss", $username, $email); | |
| $check_stmt->execute(); | |
| $check_result = $check_stmt->get_result(); | |
| if ($check_result->num_rows > 0) { | |
| $user = $check_result->fetch_assoc(); | |
| if ($user['username'] === $username) { | |
| $error = "Username already taken. Please choose another."; | |
| } else { | |
| $error = "Email already registered. Please use another email or login."; | |
| } | |
| } else { | |
| // Check if users table exists, create it if not | |
| $table_check = "SHOW TABLES LIKE 'users'"; | |
| $table_exists = $conn->query($table_check)->num_rows > 0; | |
| if (!$table_exists) { | |
| $create_table = "CREATE TABLE users ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| username VARCHAR(50) NOT NULL UNIQUE, | |
| email VARCHAR(100) NOT NULL UNIQUE, | |
| password VARCHAR(255) NOT NULL, | |
| user_type ENUM('student', 'faculty') NOT NULL, | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| last_login TIMESTAMP NULL, | |
| status ENUM('active', 'inactive') DEFAULT 'active' | |
| )"; | |
| if (!$conn->query($create_table)) { | |
| $error = "Error creating user table: " . $conn->error; | |
| } | |
| } | |
| if (empty($error)) { | |
| // Hash password | |
| $hashed_password = password_hash($password, PASSWORD_DEFAULT); | |
| // Insert new user | |
| $insert_query = "INSERT INTO users (username, email, password, user_type) VALUES (?, ?, ?, ?)"; | |
| $insert_stmt = $conn->prepare($insert_query); | |
| $insert_stmt->bind_param("ssss", $username, $email, $hashed_password, $user_type); | |
| if ($insert_stmt->execute()) { | |
| // Registration successful | |
| $_SESSION['success_message'] = "Registration successful! Please login with your credentials."; | |
| header('Location: login.php'); | |
| exit; | |
| } else { | |
| $error = "Registration failed: " . $conn->error; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // Include header | |
| include 'includes/header.php'; | |
| <div class="row"> | |
| <div class="col-md-12 mb-4 text-center"> | |
| <h2>Create a New Account</h2> | |
| <p class="lead">Join the Domain Management System</p> | |
| </div> | |
| </div> | |
| <div class="row"> | |
| <div class="col-md-6 offset-md-3"> | |
| <div class="auth-form"> | |
| <?php if (!empty($error)): ?> | |
| <div class="alert alert-danger alert-dismissible fade show" role="alert"> | |
| <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?> | |
| <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> | |
| </div> | |
| <?php endif; ?> | |
| <div class="card shadow"> | |
| <div class="card-header bg-primary text-white text-center"> | |
| <h4 class="mb-0"><i class="fas fa-user-plus me-2"></i> Register</h4> | |
| </div> | |
| <div class="card-body"> | |
| <form method="POST" action="register.php" class="needs-validation" novalidate> | |
| <div class="mb-3"> | |
| <label for="user_type" class="form-label">Register As</label> | |
| <select class="form-select" id="user_type" name="user_type" required> | |
| <option value="" selected disabled>Select user type</option> | |
| <option value="student">Student</option> | |
| <option value="faculty">Faculty</option> | |
| </select> | |
| <div class="invalid-feedback"> | |
| Please select a user type. | |
| </div> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="username" class="form-label">Username</label> | |
| <div class="input-group"> | |
| <span class="input-group-text"><i class="fas fa-user"></i></span> | |
| <input type="text" class="form-control" id="username" name="username" placeholder="Choose a username" required> | |
| </div> | |
| <div class="invalid-feedback"> | |
| Please choose a username. | |
| </div> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="email" class="form-label">Email</label> | |
| <div class="input-group"> | |
| <span class="input-group-text"><i class="fas fa-envelope"></i></span> | |
| <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email" required> | |
| </div> | |
| <div class="invalid-feedback"> | |
| Please enter a valid email address. | |
| </div> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="password" class="form-label">Password</label> | |
| <div class="input-group"> | |
| <span class="input-group-text"><i class="fas fa-lock"></i></span> | |
| <input type="password" class="form-control" id="password" name="password" placeholder="Choose a password" required minlength="6"> | |
| <button class="btn btn-outline-secondary toggle-password" type="button" data-target="#password"> | |
| <i class="fas fa-eye"></i> | |
| </button> | |
| </div> | |
| <div class="form-text">Password must be at least 6 characters long.</div> | |
| <div class="invalid-feedback"> | |
| Please enter a password (minimum 6 characters). | |
| </div> | |
| </div> | |
| <div class="mb-4"> | |
| <label for="confirm_password" class="form-label">Confirm Password</label> | |
| <div class="input-group"> | |
| <span class="input-group-text"><i class="fas fa-lock"></i></span> | |
| <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Confirm your password" required> | |
| <button class="btn btn-outline-secondary toggle-password" type="button" data-target="#confirm_password"> | |
| <i class="fas fa-eye"></i> | |
| </button> | |
| </div> | |
| <div class="invalid-feedback"> | |
| Please confirm your password. | |
| </div> | |
| </div> | |
| <div class="d-grid"> | |
| <button type="submit" class="btn btn-primary btn-lg"> | |
| <i class="fas fa-user-plus me-2"></i> Create Account | |
| </button> | |
| </div> | |
| </form> | |
| </div> | |
| <div class="card-footer text-center"> | |
| <p class="mb-0">Already have an account? <a href="login.php">Login here</a></p> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php | |
| // Include footer | |
| include 'includes/footer.php'; | |
| // Close connection | |
| $conn->close(); | |
| ?> |