34981-vm/login.php
Flatlogic Bot e59c8581a5 1.0.1
2025-10-15 18:48:09 +00:00

144 lines
5.0 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$message = '';
if (isset($_SESSION['user_id'])) {
header("Location: dashboard.php");
exit;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
$password = $_POST['password'];
$role = null;
$user_id = null;
$user_name = null;
try {
$pdo = db();
// 1. Check admins table
$stmt = $pdo->prepare("SELECT id, name, password, role FROM admins WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$role = $user['role'];
$user_id = $user['id'];
$user_name = $user['name'];
}
// 2. Check patients table (if not found in admins)
if (!$user_id) {
$stmt = $pdo->prepare("SELECT id, full_name as name, password FROM patients WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$role = 'patient';
$user_id = $user['id'];
$user_name = $user['name'];
}
}
// 3. Check doctors table (if not found yet)
if (!$user_id) {
$stmt = $pdo->prepare("SELECT id, full_name as name, password FROM doctors WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$role = 'doctor';
$user_id = $user['id'];
$user_name = $user['name'];
}
}
// 4. Check hospitals table (if not found yet)
if (!$user_id) {
$stmt = $pdo->prepare("SELECT id, hospital_name as name, password FROM hospitals WHERE contact_email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$role = 'hospital';
$user_id = $user['id'];
$user_name = $user['name'];
}
}
if ($user_id) {
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = $user_name;
$_SESSION['user_role'] = $role;
$_SESSION['user_email'] = $email;
header("Location: dashboard.php");
exit;
} else {
$message = '<div class="alert alert-danger">Invalid email or password.</div>';
}
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Medicaltour</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">Medicaltour</a>
</div>
</nav>
<!-- Page Content -->
<main class="container mt-5 pt-5">
<section id="login" class="py-5">
<div class="row justify-content-center">
<div class="col-md-5">
<h2 class="mb-4 text-center">Login</h2>
<p class="text-center text-muted">Access your dashboard.</p>
<?php if (!empty($message)) echo $message; ?>
<form action="login.php" method="post">
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="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="d-grid">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</form>
<div class="text-center mt-3">
<p>Don't have an account? <a href="index.php#register">Register here</a></p>
</div>
</div>
</div>
</section>
</main>
<!-- Footer -->
<footer class="py-5 bg-dark text-white mt-auto">
<div class="container text-center">
<p>&copy; 2025 Medicaltour. All Rights Reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>