<?php
include 'db.php';
session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 1) {
        $user = mysqli_fetch_assoc($result);
        $_SESSION['name'] = $user['name'];
        $_SESSION['email'] = $user['email'];
        header("Location: display.php");
        exit();
    } else {
        echo "<p style='color:red;'>Invalid email or password.</p>";
    }
}
?>

<link rel="stylesheet" href="style.css">

<h2>Login to Your Gallery</h2>

<form action="" method="post">
    EMAIL:<input type="email" name="email" required><br>
    PASSWORD:<input type="password" name="password" required><br>
    <input type="submit" value="Login">
</form>

<br>

<!-- 🌍 Public images (view without login) -->
<a href="public.php" class="view-public-btn">🌈 View Public Memories</a>
<br><br>

<!-- 🧑‍💻 Register link -->
<p>Don’t have an account? 
   <a href="signup.php" class="register-link">Register here</a>
</p>
