94 lines
2.8 KiB
PHP
94 lines
2.8 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$errors = [];
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = trim($_POST['email'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'A valid email is required.';
|
|
}
|
|
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id, password FROM users WHERE email = :email");
|
|
$stmt->execute(['email' => $email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
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>Login - MusicBox</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<header class="app-header">
|
|
<div class="logo">
|
|
<h1><a href="index.php">MusicBox</a></h1>
|
|
</div>
|
|
<nav>
|
|
<a href="register.php" class="btn">Register</a>
|
|
</nav>
|
|
</header>
|
|
|
|
<main class="container">
|
|
<section class="auth-form">
|
|
<h2>Login</h2>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="login.php" method="POST">
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Login</button>
|
|
</form>
|
|
</section>
|
|
</main>
|
|
|
|
<footer class="app-footer">
|
|
<p>© <?php echo date('Y'); ?> MusicBox. All rights reserved.</p>
|
|
</footer>
|
|
</body>
|
|
</html>
|