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

$email = $_POST['email'];
$password = $_POST['password'];

$sql_check = "SELECT * FROM rr_410_profile WHERE email = '$email'";
$result = $conn->query($sql_check);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $stored_hashed_password = $row['password'];
    
    if (password_verify($password, $stored_hashed_password)) {
        $_SESSION['profile_id'] = $row['profile_id'];
        $_SESSION['email'] = $email;
        header("Location: subaccount.php");
        exit();
    } else {
        echo "<script>alert('Invalid password. Please try again.'); location.href='../html/login.html'; </script>";
    }
} else {
    echo "<script>alert('Email not found. Please register first.'); location.href='../html/register.html'; </script>";
}

$conn->close();
?>
