36274-vm/login.php
Flatlogic Bot 76b95bec18 v1
2025-11-25 15:04:51 +00:00

102 lines
3.6 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
$pageTitle = "Login - Zeal Music";
$errors = [];
// If user is already logged in, redirect to dashboard
if (isset($_SESSION['user_id'])) {
header("Location: index.php");
exit;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email format.';
}
if (empty($password)) {
$errors[] = 'Password is required.';
}
if (empty($errors)) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
// Password is correct, start session
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
header("Location: index.php");
exit;
} else {
$errors[] = 'Invalid email or password.';
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($pageTitle); ?></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="dark-theme">
<div class="page-container">
<nav class="sidebar">
<div class="sidebar-header">
<a href="index.php" class="brand">Zeal Music</a>
</div>
</nav>
<main class="main-content">
<div class="content-wrapper">
<div class="upload-form-container">
<h2>Log In to Your Account</h2>
<br>
<?php if (!empty($errors)):
foreach ($errors as $error):
?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php
endforeach;
endif; ?>
<form action="login.php" method="POST" class="upload-form">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="Your email" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Your password" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Log In</button>
</div>
<p style="text-align: center; margin-top: 20px;">
Don't have an account? <a href="register.php" style="color: #1DB954;">Register Now</a>
</p>
</form>
</div>
</div>
</main>
</div>
</body>
</html>