<?php
 session_start();
 
 
 ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
 
 
 
 $conn= new mysqli('das115.truehost.cloud','honyehtx_honyehtx','B7PcCzi46xP(*8','honyehtx_mavy')or die("Could not connect to mysql".mysqli_error($conn));
 
$errors = [];

if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    // Validate email format
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = "Invalid email format!";
    }

    if (empty($errors)) {
        $query = "SELECT * FROM user WHERE email = '$email' LIMIT 1";
        $result = mysqli_query($conn, $query);

        if (mysqli_num_rows($result) === 1) {
            $user = mysqli_fetch_assoc($result);

            // Plain password comparison (for demo purposes only)
            if ($password === $user['password']) {
                $_SESSION['email'] = $user['email'];
                $_SESSION['name'] = $user['name']; // Optional

                header("Location: ../dashboard");
                exit();
            } else {
                $errors['login'] = "Incorrect password!";
            }
        } else {
            $errors['login'] = "Email not found!";
        }
    }
}





















// $errors = [];

// if (isset($_POST['login'])) {
//     $email = mysqli_real_escape_string($conn, ($_POST['email']));
//     $password = mysqli_real_escape_string($conn, $_POST['password']);

//     // Validate email format
//     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
//         $errors['email'] = "Invalid email format!";
//     }

//     // Check if user exists
//     $query = "SELECT * FROM user WHERE email = '$email' LIMIT 1";
//     $result = mysqli_query($conn, $query);

//     if (mysqli_num_rows($result) === 1) {
//         $user = mysqli_fetch_assoc($result);
//         $hashedPassword = $user['password'];

//         // Verify password
//         if (password_verify($password, $hashedPassword)) {
//             // Login successful
//             $_SESSION['email'] = $user['email'];
//             $_SESSION['name'] = $user['name']; // Optional
//             header("Location: ../dashboard");
//             exit();
//         } else {
//             $errors['login'] = "Incorrect password!";
//         }
//     } else {
//         $errors['login'] = "Email not found!";
//     }
// }
?>
