<?php
session_start();
// Redirect if already logged in
if (isset($_SESSION['user_id']) && $_SESSION['role'] === 'instructor') {
header("Location: dashboard.php");
exit;
}
// Include database configuration
require_once '../admin/database/db_config.php';
// Initialize variables
$email = '';
$errors = [];
$success = false;
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get form data
$email = trim($_POST['email']);
$password = $_POST['password'];
// Validate form data
if (empty($email)) {
$errors[] = "Email is required";
}
if (empty($password)) {
$errors[] = "Password is required";
}
// Authenticate user
if (empty($errors)) {
$query = "SELECT id, username, email, password, first_name, last_name, role, status, profile_image FROM users WHERE email = ? AND role = 'instructor'";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
// Verify password
if (password_verify($password, $user['password'])) {
// Check if account is active
if ($user['status'] === 'active') {
// Set session variables
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['email'] = $user['email'];
$_SESSION['first_name'] = $user['first_name'];
$_SESSION['last_name'] = $user['last_name'];
$_SESSION['role'] = $user['role'];
$_SESSION['profile_image'] = $user['profile_image'];
// Redirect to dashboard
header("Location: dashboard.php");
exit;
} elseif ($user['status'] === 'pending') {
$errors[] = "Your account is pending approval. Please wait for admin approval.";
} else {
$errors[] = "Your account has been suspended. Please contact the administrator.";
}
} else {
$errors[] = "Invalid email or password";
}
} else {
$errors[] = "Invalid email or password";
}
}
}
// Check for success message
if (isset($_SESSION['register_success'])) {
$success = $_SESSION['register_success'];
unset($_SESSION['register_success']);
}
// Include header
include '../includes/header.php';
?>
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h4 class="mb-0">Instructor Login</h4>
</div>
<div class="card-body">
<?php if ($success): ?>
<div class="alert alert-success">
<?php echo $success; ?>
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<ul class="mb-0">
<?php foreach ($errors as $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="post" action="">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" 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-3 form-check">
<input type="checkbox" class="form-check-input" id="remember" name="remember">
<label class="form-check-label" for="remember">Remember me</label>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</form>
<div class="mt-4 text-center">
<p>Don't have an account? <a href="register.php">Register as an Instructor</a></p>
<p><a href="forgot-password.php">Forgot your password?</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include '../includes/footer.php'; ?>