107 lines
3.9 KiB
PHP
107 lines
3.9 KiB
PHP
<?php
|
|
// Start session and load DB config right away.
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
|
|
// --- STANDARD PAGE LOAD (NON-AJAX) ---
|
|
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Include header AFTER ajax block.
|
|
include 'header.php';
|
|
|
|
// If user is already logged in, redirect to dashboard
|
|
if (isset($_SESSION['user_id'])) {
|
|
header("Location: dashboard.php");
|
|
exit;
|
|
}
|
|
|
|
$errors = [];
|
|
$registered_success = isset($_GET['registered']);
|
|
|
|
// This block now only handles the STANDARD (non-fetch) form submission.
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
echo '<div class="alert alert-info">DEBUG: Standard POST handler reached.</div>';
|
|
echo '<pre class="alert alert-warning">DEBUG: $_POST data: ' . htmlspecialchars(print_r($_POST, true)) . '</pre>';
|
|
|
|
$email = trim($_POST['login_email']);
|
|
$password = $_POST['login_pass'];
|
|
|
|
if (empty($email)) {
|
|
$errors[] = 'Email is required.';
|
|
}
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id, display_name, password_hash FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password_hash'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['display_name'] = $user['display_name'];
|
|
// Standard PHP redirect
|
|
header("Location: dashboard.php");
|
|
exit;
|
|
} else {
|
|
$errors[] = 'Invalid email or password.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log("Database error in login.php (Standard): " . $e->getMessage());
|
|
$errors[] = 'A server error occurred. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<main class="container flex-grow-1 d-flex align-items-center justify-content-center">
|
|
<div class="col-md-6 col-lg-4">
|
|
<div class="card my-5">
|
|
<div class="card-body p-4">
|
|
<h1 class="card-title text-center mb-4">Sign In</h1>
|
|
|
|
<div id="alert-container">
|
|
<?php if ($registered_success): ?>
|
|
<div class="alert alert-success" role="alert">
|
|
Registration successful! You can now sign in.
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<form id="login-form" method="POST" action="login.php">
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email address</label>
|
|
<input type="email" class="form-control" id="email" name="login_email" placeholder="name@example.com" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" class="form-control" id="password" name="login_pass" required>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">Sign In</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<div class="card-footer text-center py-3">
|
|
<small class="text-muted">Don't have an account? <a href="signup.php">Sign Up</a></small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include 'footer.php'; ?>
|